github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/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 _Added in gomplate [v3.3.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.3.0)_ 17 ### Usage 18 19 ``` 20 tmpl.Exec name [context] 21 ``` 22 ``` 23 context | tmpl.Exec name 24 ``` 25 26 ### Arguments 27 28 | name | description | 29 |------|-------------| 30 | `name` | _(required)_ The template's name. | 31 | `context` | _(optional)_ The context to use. | 32 33 ### Examples 34 35 ```console 36 $ gomplate -i '{{define "T1"}}hello, world!{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}' 37 HELLO, WORLD! 38 ``` 39 ```console 40 $ gomplate -i '{{define "T1"}}hello, {{.}}{{end}}{{ tmpl.Exec "T1" "world!" | strings.Title }}' 41 Hello, World! 42 ``` 43 44 ## `tmpl.Inline` 45 46 **Alias:** `tpl` 47 48 Render the given string as a template, just like a nested template. 49 50 If the template is given a name (see `name` argument below), it can be re-used later with the `template` keyword. 51 52 A context can be provided, otherwise the default gomplate context will be used. 53 54 _Added in gomplate [v3.3.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.3.0)_ 55 ### Usage 56 57 ``` 58 tmpl.Inline [name] in [context] 59 ``` 60 61 ### Arguments 62 63 | name | description | 64 |------|-------------| 65 | `name` | _(optional)_ The template's name. | 66 | `in` | _(required)_ The template to render, as a string | 67 | `context` | _(optional)_ The context to use when rendering - this becomes `.` inside the template. | 68 69 ### Examples 70 71 ```console 72 $ gomplate -i '{{ tmpl.Inline "{{print `hello world`}}" }}' 73 hello world 74 ``` 75 ```console 76 $ gomplate -i ' 77 {{ $tstring := "{{ print .value ` world` }}" }} 78 {{ $context := dict "value" "hello" }} 79 {{ tpl "T1" $tstring $context }} 80 {{ template "T1" (dict "value" "goodbye") }} 81 ' 82 hello world 83 goodbye world 84 ``` 85 86 ## `tmpl.Path` 87 88 Output the path of the current template, if it came from a file. For 89 inline templates, this will be an empty string. 90 91 Note that if this function is called from a nested template, the path 92 of the main template will be returned instead. 93 94 _Added in gomplate [v3.11.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.11.0)_ 95 ### Usage 96 97 ``` 98 tmpl.Path 99 ``` 100 101 102 ### Examples 103 104 _`subdir/input.tpl`:_ 105 ``` 106 this template is in {{ tmpl.Path }} 107 ``` 108 109 ```console 110 $ gomplate -f subdir/input.tpl 111 this template is in subdir/input.tpl 112 ``` 113 114 ## `tmpl.PathDir` 115 116 Output the current template's directory. For inline templates, this will 117 be an empty string. 118 119 Note that if this function is called from a nested template, the path 120 of the main template will be used instead. 121 122 _Added in gomplate [v3.11.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.11.0)_ 123 ### Usage 124 125 ``` 126 tmpl.PathDir 127 ``` 128 129 130 ### Examples 131 132 _`subdir/input.tpl`:_ 133 ``` 134 this template is in {{ tmpl.Dir }} 135 ``` 136 137 ```console 138 $ gomplate -f subdir/input.tpl 139 this template is in subdir 140 ```