Javascript輕量極速的模板引擎,支持預(yù)編譯,支持嵌入原生javascript語法,忽略不存在的變量,可直接在AMD、CMD以及全局環(huán)境中使用。具體代碼如下:
HTML代碼:
<div id="view"></div>
<script type="text/template" id="demo">
<p>Hi, I'm <%=author%><%=anything%></p>
<p>I can write:
<%
for(var i=0; i<languages.length; i++) {
echo( (i===0?"":", ") + languages[i] );
}
%>
</p>
</script>
直接使用:
//模擬數(shù)據(jù)
var data = {
author: "Jony",
languages: ["javascript", "php", "shell"]
};
document.getElementById('view').innerHTML = tpl('#demo', data);
預(yù)編譯:
//模擬數(shù)據(jù)
var data = {
author: "Jony",
languages: ["javascript", "php", "shell"]
};
//編譯后就可以無限調(diào)用
var compiler = tpl('#demo');
document.getElementById('view').innerHTML = compiler(data);
HTML結(jié)果:
<p>Hi, I'm Jony</p>
<p>I can write: javascript, php, shell</p>
更多信息請(qǐng)查看IT技術(shù)專欄