Lightweight javascript template engine. Adaptation AMD, CMD and global environment
- Include
tpl.js
:
<script type="text/javascript" src="path/to/tpl.js"></script>
Or in sea.js, you can include by: var tpl = require('path/to/tpl');
- Template in your html:
<script type="text/template" id="demo">
<p>Hi, I'm <#=author#></p>
<p>I can write:
<# for(var i=0; i<languages.length; i++) { #>
<#=(i===0?"":",")+languages[i]#>
<#}#>
</p>
</script>
Or use internal method echo()
:
<script type="text/template" id="demo">
<p>Hi, I'm <#=author#></p>
<p>I can write:
<#
for(var i=0; i<languages.length; i++) {
echo( (i===0?"":", ") + languages[i] );
}#>
</p>
</script>
- Execution rendering:
console.log( tpl('#demo', {author: "Jony", languages: ["javascript", "php", "shell"]}) );
- Result is:
<p>Hi, I'm Jony</p>
<p>I can write: javascript, php, shell</p>
template
can be a html template string or '#' plus an id, This will return a rendered html string.
- IE6+
- Chrome
- Safari 4+
- Firefox 3.5+
- Opera
- Report a bug
- To contribute or send an idea, github message me or fork the project
tpl.js use UglifyJS2
you should have installed nodejs and run npm install uglify-js -g
.
On Windows, you can run build.bat
from root directory and it will package tpl.debug.js
into tpl.js
.
tpl.js is available under the terms of the MIT License.