github.com/wuhuizuo/gomplate@v3.5.0+incompatible/docs/content/functions/tmpl.md (about) 1 --- 2 title: template functions 3 menu: 4 main: 5 parent: functions 6 --- 7 8 Functions for defining or executing templates. 9 10 ## `tmpl.Exec` 11 12 Execute (render) the named template. This is equivalent to using the [`template`](https://golang.org/pkg/text/template/#hdr-Actions) action, except the result is returned as a string. 13 14 This allows for post-processing of templates. 15 16 ### Usage 17 18 ```go 19 tmpl.Exec name [context] 20 ``` 21 ```go 22 context | tmpl.Exec name 23 ``` 24 25 ### Arguments 26 27 | name | description | 28 |------|-------------| 29 | `name` | _(required)_ The template's name. | 30 | `context` | _(optional)_ The context to use. | 31 32 ### Examples 33 34 ```console 35 $ gomplate -i '{{define "T1"}}hello, world!{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}' 36 HELLO, WORLD! 37 ``` 38 ```console 39 $ gomplate -i '{{define "T1"}}hello, {{.}}{{end}}{{ tmpl.Exec "T1" "world!" | strings.Title }}' 40 Hello, World! 41 ``` 42 43 ## `tmpl.Inline` 44 45 **Alias:** `tpl` 46 47 Render the given string as a template, just like a nested template. 48 49 If the template is given a name (see `name` argument below), it can be re-used later with the `template` keyword. 50 51 A context can be provided, otherwise the default gomplate context will be used. 52 53 ### Usage 54 55 ```go 56 tmpl.Inline [name] in [context] 57 ``` 58 59 ### Arguments 60 61 | name | description | 62 |------|-------------| 63 | `name` | _(optional)_ The template's name. | 64 | `in` | _(required)_ The template to render, as a string | 65 | `context` | _(optional)_ The context to use when rendering - this becomes `.` inside the template. | 66 67 ### Examples 68 69 ```console 70 $ gomplate -i '{{ tmpl.Inline "{{print `hello world`}}" }}' 71 hello world 72 ``` 73 ```console 74 $ gomplate -i ' 75 {{ $tstring := "{{ print .value ` world` }}" }} 76 {{ $context := dict "value" "hello" }} 77 {{ tpl "T1" $tstring $context }} 78 {{ template "T1" (dict "value" "goodbye") }} 79 ' 80 hello world 81 goodbye world 82 ```