0.0.7 • Published 9 years ago
rtt v0.0.7
rtt
模板替换工具 rtt --- replace templete tool
###安装
npm install rtt --save-dev
###用法: 在项目目录中添加rtt.js
var rtt = require('rtt');
//从a.src.html读取模板, 从data.json读取数据, 生成到a.html文件
//第四个参数设置src文件中变量名(默认是data.json的文件名,可不传), 第五个参数设置定界符(默认<%%>, 可不传)
rtt('a.src.html', 'a.html', 'data.json');
然后cmd或终端中切换到rtt.js所在目录执行node rtt
在data.json
{
"name": "steve",
"project":[{
"name": "tom",
"describe": "this is a test"
}, {
"name": "jim",
"describe": "this is a test"
}, {
"name": "lucy",
"describe": "this is a test"
}]
}
在a.src.html中
<!DOCTYPE html>
<html>
<head>
<title><%=data.name%></title>
<meta charset="utf-8">
</head>
<body>
<ul>
<% for(var i=0, m=data.project.length; i++){ %>
<li><%=data.project[i].name%> <span> <%=data.project[i].describe%> </span></li>
<% } %>
</ul>
</body>
</html>
在a.html文件中输出
<html>
<head>
<title><%=data.name%></title>
<meta charset="utf-8">
</head>
<body>
<ul>
<li>tom <span>this is a test</span></li>
<li>jim <span>this is a test</span></li>
<li>lucy <span>this is a test</span></li>
</ul>
</body>
</html>
另外, 此工具允许使用include引如其他文件, 例如: index.src.html
<!DOCTYPE html>
<html>
<head>
<title><%=data.name%></title>
<meta charset="utf-8">
</head>
<body>
<div>
<%include('tpl/a.html')%>
</div>
<p>goodebye</p>
</body>
</html>
在tpl/a.html中:
<ul>
<li>haha</li>
<li>hehe</li>
<li>
<%include('c.html')%>
</li>
</ul>
在tpl/c.html中
<div>I'm in c.html</div>
最终输出到index.html中内容如下:
<!DOCTYPE html>
<html>
<head>
<title><%=data.name%></title>
<meta charset="utf-8">
</head>
<body>
<div>
<ul>
<li>haha</li>
<li>hehe</li>
<li>
<div>I'm in c.html</div>
</li>
</ul>
</div>
<p>goodebye</p>
</body>
</html>
###协议: MIT