tpl.jsJavaScript 模板引擎

联合创作 · 2023-09-29 01:49

tpl-js 是一个轻量的(仅1kb)、极速的 javascript 模板引擎。支持预编译,支持嵌入原生javascript语法,忽略不存在的变量,可直接在AMD、CMD以及全局环境中使用。


使用方法:


1.引入tpl.js



<script type="text/javascript" src="path/to/tpl.js"></script>

或者在Sea.js和Node.js中:



var tpl = require('path/to/tpl');

2.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>

3.执行渲染



console.log( tpl('#demo', {author: "Jony", languages:["javascript", "php", "shell"]}) );

 结果为: 



<p>Hi, I'm Jony</p>
<p>I can write: javascript, php, shell</p>

 


在线例子:http://niceue.github.io/tpl.js/


在线文档:https://github.com/niceue/tpl.js 

浏览 16
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报