render takes a map of templates/values and renders them. The err return is named on purpose: the deferred recover below assigns to it.
(tpls map[string]renderable)
| 348 | // render takes a map of templates/values and renders them. The err return is |
| 349 | // named on purpose: the deferred recover below assigns to it. |
| 350 | func (e Engine) render(tpls map[string]renderable) (_ map[string]string, err error) { |
| 351 | // Basically, what we do here is start with an empty parent template and then |
| 352 | // build up a list of templates -- one for each file. Once all of the templates |
| 353 | // have been parsed, we loop through again and execute every template. |
| 354 | // |
| 355 | // The idea with this process is to make it possible for more complex templates |
| 356 | // to share common blocks, but to make the entire thing feel like a file-based |
| 357 | // template engine. |
| 358 | defer func() { |
| 359 | if r := recover(); r != nil { |
| 360 | err = errors.Errorf("rendering template failed: %v", r) |
| 361 | } |
| 362 | }() |
| 363 | |
| 364 | tmpl := template.New("gotpl") |
| 365 | if e.Strict { |
| 366 | tmpl.Option("missingkey=error") |
| 367 | } else { |
| 368 | // Not that zero will attempt to add default values for types it knows, |
| 369 | // but will still emit <no value> for others. We mitigate that later. |
| 370 | tmpl.Option("missingkey=zero") |
| 371 | } |
| 372 | |
| 373 | e.initFunMap(tmpl) |
| 374 | |
| 375 | // We want to parse the templates in a predictable order. The order favors |
| 376 | // higher-level (in file system) templates over deeply nested templates. |
| 377 | keys := sortTemplates(tpls) |
| 378 | |
| 379 | for _, filename := range keys { |
| 380 | r := tpls[filename] |
| 381 | |
| 382 | _, err := tmpl.New(filename).Parse(r.tpl) |
| 383 | if err != nil { |
| 384 | return map[string]string{}, cleanupParseError(filename, err) |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | rendered := make(map[string]string, len(keys)) |
| 389 | for _, filename := range keys { |
| 390 | // Don't render partials. We don't care out the direct output of partials. |
| 391 | // They are only included from other templates. |
| 392 | if strings.HasPrefix(path.Base(filename), "_") { |
| 393 | continue |
| 394 | } |
| 395 | // At render time, add information about the template that is being rendered. |
| 396 | vals := tpls[filename].vals |
| 397 | vals["Template"] = chartutil.Values{"Name": filename, "BasePath": tpls[filename].basePath} |
| 398 | |
| 399 | var buf strings.Builder |
| 400 | |
| 401 | err := tmpl.ExecuteTemplate(&buf, filename, vals) |
| 402 | if err != nil { |
| 403 | return map[string]string{}, cleanupExecError(filename, err) |
| 404 | } |
| 405 | |
| 406 | // Work around the issue where Go will emit "<no value>" even if Options(missing=zero) |
| 407 | // is set. Since missing=error will never get here, we do not need to handle |