< 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.
## Usage
### Client-side
Include the (minified) JavaScript Templates script in your HTML markup:
```html
<script src="js/tmpl.min.js"></script>
```
Add a script section with type **"text/x-tmpl"**, a unique **id** property and your template definition as content:
**tmpl.encode** makes use of the regular expression **tmpl.encReg** and the encoding map **tmpl.encMap** to match and replace special characters, which can be modified to change the behavior of the output encoding.
Strings matched by the regular expression, but not found in the encoding map are removed from the output. This allows for example to automatically trim input values (removing whitespace from the start and end of the string):
The local variables available inside the templates are the following:
***o**: The data object given as parameter to the template function (see the next section on how to modify the parameter name).
***tmpl**: A reference to the **tmpl** function object.
***_s**: The string for the rendered result content.
***_e**: A reference to the **tmpl.encode** method.
***print**: Helper function to add content to the rendered result string.
***include**: Helper function to include the return value of a different template in the result.
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):
The generated template functions accept one argument, which is the data object given to the **tmpl(id, data)** function. This argument is available inside the template definitions as parameter **o** (the lowercase letter).
The argument name can be modified by overriding **tmpl.arg**:
The template contents are matched and replaced using the regular expression **tmpl.regexp** and the replacement function **tmpl.func**. The replacement function operates based on the [parenthesized submatch strings](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter).
To use different tags for the template syntax, override **tmpl.regexp** with a modified regular expression, by exchanging all occurrences of "{%" and "%}", e.g. with "[%" and "%]":
By default, the plugin preserves whitespace (newlines, carriage returns, tabs and spaces). To strip unnecessary whitespace, you can override the **tmpl.func** function, e.g. with the following code:
```js
varoriginalFunc=tmpl.func;
tmpl.func=function(s,p1,p2,p3,p4,p5,offset,str){
if(p1&&/\s/.test(p1)){
if(!offset||/\s/.test(str.charAt(offset-1))||
/^\s+$/g.test(str.slice(offset))){
return'';
}
return'';
}
returnoriginalFunc.apply(tmpl,arguments);
};
```
## Templates syntax
### Interpolation
Print variable with HTML special characters escaped:
```html
<h3>{%=o.title%}</h3>
```
Print variable without escaping:
```html
<h3>{%#o.user_id%}</h3>
```
Print output of function calls:
```html
<ahref="{%=encodeURI(o.url)%}">Website</a>
```
Use dot notation to print nested properties:
```html
<strong>{%=o.author.name%}</strong>
```
### Evaluation
Use **print(str)** to add escaped content to the output:
```html
<span>Year: {% var d=new Date(); print(d.getFullYear()); %}</span>
```
Use **print(str, true)** to add unescaped content to the output:
```html
<span>{% print("Fast & powerful", true); %}</span>
```
Use **include(str, obj)** to include content from a different template:
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.
The compilation script is built for [node.js](http://nodejs.org/) and also requires [UglifyJS](https://github.com/mishoo/UglifyJS).
To use it, first install both the JavaScript Templates project and UglifyJS via [npm](https://www.npmjs.org/):
```sh
npm install uglify-js
npm install blueimp-tmpl
```
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).
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.
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.
## Tests
The JavaScript Templates project comes with [Unit Tests](https://en.wikipedia.org/wiki/Unit_testing).
There are two different ways to run the tests:
* Open test/index.html in your browser or
* run `npm test` in the Terminal in the root path of the repository package.
The first one tests the browser integration, the second one the [node.js](http://nodejs.org/) integration.
## License
The JavaScript Templates script is released under the [MIT license](http://www.opensource.org/licenses/MIT).