You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+31-2Lines changed: 31 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -145,14 +145,38 @@ This is done by calling the following functions from within templates and script
145
145
*`templateString(templateFile string, data any) (string, error)` - Start a template file and return the rendered template as a string.
146
146
*`templateFile` (string) - The path to the template file to start the engine from.
147
147
*`data` (any) - Context data to provide to templates and scripts. Available as `{{.Local}}` in templates and `context.Local` in scripts.
148
+
*`templateStringInput(templateName string, templateString string, data any) (string, error)` - Template the input string and return the rendered template as a string.
149
+
*`templateName` (string) - The name of the template to render.
150
+
*`templateString` (string) - An input template string to template.
151
+
*`data` (any) - Context data to provide to templates and scripts. Available as `{{.Local}}` in templates and `context.Local` in scripts.
152
+
*`recurse(recursions int) string` - Recurse the current template file, recursions is the number of times to recurse the template file.
153
+
*`recursions` (int) - The number of times to recurse the template file.
148
154
149
155
This allows for example:
150
156
151
157
```gotemplate
152
158
{{ templateFile "tmpl.stmpl" "out.txt" .Local }}{{/* Template another file */}}
153
159
{{ templateString "tmpl.stmpl" .Local }}{{/* Template another file and include the rendered output in this templates rendered output */}}
160
+
{{ templateStringInput "Hello {{ .Local.name }}" .Local }}{{/* Template a string and include the rendered output in this templates rendered output */}}
154
161
```
155
162
163
+
#### Recursive templating
164
+
165
+
It is possible with the `recurse` function in a template to render the same template multiple times. This can be useful when data to render parts of the template are only available after you have rendered it at least once.
166
+
167
+
For example:
168
+
169
+
```go
170
+
{{- recurse 1 -}}
171
+
{{"{{.RecursiveComputed.Names}}"}}{{/* Render the names of the customers after we have iterated over them later */}}
Note: The `recurse` function must be called as the first thing in the template on its own line.
179
+
156
180
### Registering templating functions
157
181
158
182
The engine allows you to register custom templating functions from Go which can be used within the templates.
@@ -184,8 +208,9 @@ sjs```
184
208
The `sjs` snippet can be used anywhere within your template (including multiple snippets) and will be replaced with any "rendered" output returned when using the `render` function.
185
209
186
210
Naive transformation of typescript code is supported through [esbuild](https://esbuild.github.io/api/#transformation). This means that you can directly import typescript code and use type annotations in place of any JavaScript. However, be aware:
187
-
* EasyTemplate will not perform type checking itself. Type annotations are transformed into commented out code.
188
-
* Scripts/Snippets are not bundled, but executed as a single module on the global scope. This means no `import` statements are possible. [Instead, the global `require` function](#importing-javascript) is available to directly execute JS/TS code.
211
+
212
+
* EasyTemplate will not perform type checking itself. Type annotations are transformed into commented out code.
213
+
* Scripts/Snippets are not bundled, but executed as a single module on the global scope. This means no `import` statements are possible. [Instead, the global `require` function](#importing-javascript) is available to directly execute JS/TS code.
189
214
190
215
### Context data
191
216
@@ -305,6 +330,10 @@ The following functions are available to JavaScript from the templating engine:
305
330
*`templateString(templateString, data)` - Render a template and return the rendered output.
306
331
*`templateString` (string) - The template string to render.
307
332
*`data` (object) - Data available to the template as `Local` context ie `{name: "John"}` is available as `{{ .Local.name }}`.
333
+
*`templateStringInput(templateName, templateString, data)` - Render a template and return the rendered output.
334
+
*`templateName` (string) - The name of the template to render.
335
+
*`templateString` (string) - The template string to render.
336
+
*`data` (object) - Data available to the template as `Local` context ie `{name: "John"}` is available as `{{ .Local.name }}`.
308
337
*`render(output)` - Render the output to the template file, if called multiples times the output will be appended to the previous output as a new line. The cumulative output will replace the current `sjs` block in the template file.
309
338
*`output` (string) - The output to render.
310
339
*`require(filePath)` - Import a JavaScript file into the global scope.
0 commit comments