JS之模板技术(aui / artTemplate)
来源:互联网 发布:arc map空间数据编辑 编辑:程序博客网 时间:2024/05/23 12:49
artTemplate是个好东西啊,一个开源的js前端模板引擎,使用简单,渲染效率特别的高。
我经常使用这个技术来在前端动态生成新闻列表,排行榜,历史记录等需要在前端列表显示的信息。
下面是artTemplate的下载链接:
https://github.com/aui/artTemplate
因为artTemplate比较简单,容易上手,项目的例子,文档又比较齐全,大家有需要可以直接参考官方文档,例子进行深入了解,
我这里就这是用简单常用的,用于快速上手的一个例子吧!
先说明,我是下载artTemplate工程项目src目录下的template.js的
内容大概为:
...略
var template = function (id, content) {
return template[
typeof content === 'object' ? 'render' : 'compile'
].apply(template, arguments);
};
...略
其中主要也就是使用到这个函数。
前端的页面内容主要为
<body>
<center><h1><font color="#f00">这是template模板技术使用示例</font></h1></center>
<script id="personListId" type="text/html">
<font color="#f00" size="24">
<$for (var i = 0; i < personList.length; i++) {$>
客户姓名:<$=personList[i].name$> 客户年龄:<$=personList[i].age$><br/>
<$}$>
</font>
</script>
<div id="templateContent"></div>
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="js/template.js"></script>
<script type="text/javascript" src="js/page/index.js"></script>
</body>
其中我使用了jquery,template,这两个都可以上网下载,放置到对应目录就ok。
下面这段代码使用模板技术进行for循环,格式为:
<$$>对内可写js代码,<$=val$>是输出js的变量val的值,
看着这个for循环,需要注意三点:
1)<script></script>必须标上唯一id,如<script id="personListId"></script>
2)<script></script>的type的值是text/html,而不是text/javascript
3)personList这个js变量从哪里来的,这里先留个疑问吧
4)对于这个列表要怎么显示,你就对应怎么写就好,这里是最简单的显示客户姓名和客户年龄,也没带什么图片,样式之类的
客户姓名:<$=personList[i].name$> 客户年龄:<$=personList[i].age$><br/>。
<script id="personList" type="text/html">
<font color="#f00" size="24">
<$for (var i = 0; i < personList.length; i++) {$>
客户姓名:<$=personList[i].name$> 客户年龄:<$=personList[i].age$><br/>
<$}$>
</font>
</script>
接下来就是写自己的js代码,使用template模板技术,动态渲染以上前端代码
代码写在js/page/index.js这个文件中,内容为:
$(function(){
var persons= [
{
name : "11111111111",
age : 1111111111111111
},
{
name : "2222222222",
age : 2222222222
},
{
name : "33333333333",
age : 333333333333
}
];//自定义的json格式数据,实际应用中一般都是使用ajax请求服务器获取json格式的数据,不知道从js的哪个版本起,js已经内置支持json格式的数据
var html = template('personListId',{personList : persons});//看着这行代码,是否注意到之前提到的personListId和personList 已经在这里使用上和定义好了
$('#templateContent').html('').html(html);//jquery的用法,目的就是将动态生成的内容(html)填充到id为templateContent的div
});
write less,do more,i like
- JS之模板技术(aui / artTemplate)
- JS之模板技术(aui / artTemplate)
- 高性能js模板引擎(artTemplate )
- JS模板引擎 :ArtTemplate
- 前端模板引擎arttemplate.js 学习之include方法
- arttemplate js模板的使用
- 使用artTemplate模板开发网站(node.js + express环境)
- 浅谈模板引擎之artTemplate
- 腾讯js模板引擎 artTemplate 学习
- JS模板引擎-腾讯artTemplate 总结
- js模板引擎artTemplate学习总结
- JS模板引擎---腾讯artTemplate的使用
- 关于js模板引擎 arttemplate 的使用
- js模板引擎artTemplate快速上手
- arttemplate模板
- arttemplate.js
- JS 前端框架1 artTemplate前端js模板引擎
- js高效模板引擎artTemplate 的使用总结
- 今天发现一个bug,不知道是什么问题,printf的问题吗,还是什么。先记下!
- <转载>CentOS 6.3下Samba服务器的安装与配置
- Qualcomm(高通)
- foxmail邮件发送不成功
- [C#]將數字前面補0,補足設定的長度
- JS之模板技术(aui / artTemplate)
- 32位系统和64位系统变量所占空间总结
- ie678兼容css3
- C/C++ 初学简单笔记 —3— 继承与派生
- 配置ssh无密码登陆的错误
- OpenCV 2.4.X最常用的数据类型
- 编程挑战之三角形的内切圆与外接圆
- 【C/C++】计时函数比较
- linux sar命令详解