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
The local variables available inside the templates are the following:
175
176
176
177
***o**: The data object given as parameter to the template function (see the next section on how to modify the parameter name).
178
+
***tmpl**: A reference to the **tmpl** function object.
177
179
***_s**: The string for the rendered result content.
178
-
***_t**: A reference to the **tmpl** function object.
179
180
***_e**: A reference to the **tmpl.encode** method.
180
-
***print**: Function to add content to the rendered result string.
181
-
***include**: Function to include the return value of a different template in the result.
181
+
***print**: Helper function to add content to the rendered result string.
182
+
***include**: Helper function to include the return value of a different template in the result.
182
183
183
184
To introduce additional local helper variables, the string **tmpl.helper** can be extended. The following adds a convenience function for *console.log* and a streaming function, that streams the template rendering result back to the callback argument (note the comma at the beginning of each variable declaration):
Those new helper functions could be used to stream the template contents to the console output:
191
192
192
193
```html
193
-
<scripttype="text/html"id="tmpl-demo">
194
+
<scripttype="text/x-tmpl"id="tmpl-demo">
194
195
<h3>{%=o.title%}</h3>
195
196
{%stream(log); %}
196
197
<p>Released under the
@@ -305,5 +306,27 @@ For loop:
305
306
</ul>
306
307
```
307
308
309
+
## Compiled templates
310
+
The JavaScript Templates project comes with a compilation script, that allows you to compile your templates into JavaScript code and combine them with a minimal Templates runtime into one minified JavaScript file.
311
+
312
+
The compilation script is built for [node.js](http://nodejs.org/) and also requires [UglifyJS](https://github.com/mishoo/UglifyJS).
313
+
To use it, first install both the JavaScript Templates project and UglifyJS via [npm](http://npmjs.org/):
314
+
315
+
```sh
316
+
npm install uglify-js
317
+
npm install blueimp-tmpl
318
+
```
319
+
320
+
This will put the executables **uglifyjs** and **tmpl.js** into the folder **node_modules/.bin**. It will also make them available on your PATH if you install the packages globally (by adding the **-g** flag to the install command).
321
+
322
+
The **tmpl.js** executable accepts the paths to one or multiple template files as command line arguments and prints the generated JavaScript code to the console output. The following command line shows you how to store the generated code in a new JavaScript file that can be included in your project:
The files given as command line arguments to **tmpl.js** can either be pure template files or HTML documents with embedded template script sections. For the pure template files, the file names (without extension) serve as template ids.
329
+
The generated file can be included in your project as a replacement for the original **tmpl.js** runtime. It provides you with the same API and provides a **tmpl(id, data)** function that accepts the id of one of your templates as first and a data object as optional second parameter.
330
+
308
331
## License
309
332
The JavaScript Templates script is released under the [MIT license](http://www.opensource.org/licenses/MIT).
Copy file name to clipboardExpand all lines: package.json
+6-2Lines changed: 6 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
{
2
2
"name": "blueimp-tmpl",
3
-
"version": "1.0.2",
3
+
"version": "2.0.0",
4
4
"title": "JavaScript Templates",
5
5
"description": "< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.",
0 commit comments