template.js 模版内调用外部JS方法

template.js 一款 JavaScript 模板引擎,简单,好用。提供一套模板语法,用户能够写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片断,渲染不一样的效果。
模版定义以下:javascript

<script type="text/html" id="list">
	<p>
		<span>{{price}}</span>
		<span style="float:right">{{costTypeName}}</span>
	</p>
</script>

 {{}}里面的属性表明将被替换的数据,使用方法以下:html

html = template("list", {price:200,costTypeName:"测试"});
选而后的html为:
<p>
<span>200</span>
<span style="float:right">测试</span>
</p>java

但是若是咱们想要让price的200变为¥200.00怎么办呢?只需以下几步便可
1.建立全局帮助函数git

template.helper("fromatMoney", function (x) {
	return "¥" + Duanjt.Float.ToFloat(x, 2);
});

 

2.修改模版github

<script type="text/html" id="list">
	<p>
		<span>{{fromatMoney price}}</span>
		<span style="float:right">{{costTypeName}}</span>
	</p>
</script>

注意:须要引入template.js文件 开源地址: https://github.com/aui/artTemplate函数

相关文章
相关标签/搜索