underscore中template的使用Demo

在客户端渲染数据时,一般可通过underscore中的template对数据模板进行渲染,例如:

定义模板,需要把type类型设置为“text/template”

<script type="text/template" id="tpl">
<% _.each(data, function (item) { %>
  <div class="outer">
    <%= item.title %> - <%= item.url %> - <%= item.film %>
  </div>
<% }); %>
</script>

在js调用模板的时候,传入模板数据,例如:

var datas = [
  {
    title: ‘标题1‘,
    url: ‘http://www.baidu.com‘,
    film: ‘电影名称1‘
  },
  {
    title: ‘标题2‘,
    url: ‘http://www.baidu.com‘,
    film: ‘电影名称2‘
  },
  {
    title: ‘标题3‘,
    url: ‘http://www.baidu.com‘,
    film: ‘电影名称3‘
  },
  {
    title: ‘标题4‘,
    url: ‘http://www.baidu.com‘,
    film: ‘电影名称4‘
  },
  {
    title: ‘标题5‘,
    url: ‘http://www.baidu.com‘,
    film: ‘电影名称5‘
  }
  ,
  {
    title: ‘标题6666‘,
    url: ‘http://www.baidu.com‘,
    film: ‘电影名称5‘
  }
];

var t=_.template($("#tpl").html());
$(‘#content‘).html(t({"data":datas}));  //这里把模板中的数据源取别名为data,则模板中的数据源也用这个别名,与此保持一致

例如:根据拼音首字母对城市进行分组,代码如下:

首先数据源文件city.js中的数据格式如下;

{"status":0,"msg":"","data":[{"cityId":340800,"cityName":"\u5b89\u5e86\u5e02","cityPinYin":"anqingshi"},{"cityId":210300,"cityName":"\u978d\u5c71\u5e02","cityPinYin":"anshanshi"},{"cityId":410500,"cityName":"\u5b89\u9633\u5e02","cityPinYin":"anyangshi"},{"cityId":340300,"cityName":"\u868c\u57e0\u5e02","cityPinYin":"bangbushi"},{"cityId":130600,"cityName":"\u4fdd\u5b9a\u5e02","cityPinYin":"baodingshi"},{"cityId":110100,"cityName":"\u5317\u4eac\u5e02","cityPinYin":"beijingshi"},{"cityId":530500,"cityName":"\u4fdd\u5c71\u5e02","cityPinYin":"baoshanshi"},{"cityId":150200,"cityName":"\u5305\u5934\u5e02","cityPinYin":"baotoushi"},{"cityId":341600,"cityName":"\u4eb3\u5dde\u5e02","cityPinYin":"bozhoushi"},{"cityId":220100,"cityName":"\u957f\u6625\u5e02","cityPinYin":"changchunshi"},{"cityId":430700,"cityName":"\u5e38\u5fb7\u5e02","cityPinYin":"changdeshi"},{"cityId":510100,"cityName":"\u6210\u90fd\u5e02","cityPinYin":"chengdoushi"},{"cityId":150400,"cityName":"\u8d64\u5cf0\u5e02","cityPinYin":"chifengshi"},{"cityId":500100,"cityName":"\u91cd\u5e86\u5e02","cityPinYin":"chongqingshi"},{"cityId":430100,"cityName":"\u957f\u6c99\u5e02","cityPinYin":"changshashi"},{"cityId":130900,"cityName":"\u6ca7\u5dde\u5e02","cityPinYin":"cangzhoushi"},{"cityId":320400,"cityName":"\u5e38\u5dde\u5e02","cityPinYin":"changzhoushi"},{"cityId":431000,"cityName":"\u90f4\u5dde\u5e02","cityPinYin":"chenzhoushi"},{"cityId":210600,"cityName":"\u4e39\u4e1c\u5e02","cityPinYin":"dandongshi"},{"cityId":441900,"cityName":"\u4e1c\u839e\u5e02","cityPinYin":"dongguanshi"},{"cityId":210200,"cityName":"\u5927\u8fde\u5e02","cityPinYin":"dalianshi"},{"cityId":230600,"cityName":"\u5927\u5e86\u5e02","cityPinYin":"daqingshi"},{"cityId":370500,"cityName":"\u4e1c\u8425\u5e02","cityPinYin":"dongyingshi"},{"cityId":371400,"cityName":"\u5fb7\u5dde\u5e02","cityPinYin":"dezhoushi"},{"cityId":440600,"cityName":"\u4f5b\u5c71\u5e02","cityPinYin":"foshanshi"},{"cityId":210400,"cityName":"\u629a\u987a\u5e02","cityPinYin":"fushunshi"},{"cityId":341200,"cityName":"\u961c\u9633\u5e02","cityPinYin":"fuyangshi"},{"cityId":361000,"cityName":"\u629a\u5dde\u5e02","cityPinYin":"fuzhoushi"},{"cityId":350100,"cityName":"\u798f\u5dde\u5e02","cityPinYin":"fuzhoushi"},{"cityId":450300,"cityName":"\u6842\u6797\u5e02","cityPinYin":"guilinshi"},{"cityId":510800,"cityName":"\u5e7f\u5143\u5e02","cityPinYin":"guangyuanshi"},{"cityId":640400,"cityName":"\u56fa\u539f\u5e02","cityPinYin":"guyuanshi"},{"cityId":440100,"cityName":"\u5e7f\u5dde\u5e02","cityPinYin":"guangzhoushi"},{"cityId":360700,"cityName":"\u8d63\u5dde\u5e02","cityPinYin":"ganzhoushi"},{"cityId":320800,"cityName":"\u6dee\u5b89\u5e02","cityPinYin":"huaianshi"},{"cityId":130400,"cityName":"\u90af\u90f8\u5e02","cityPinYin":"handanshi"},{"cityId":230100,"cityName":"\u54c8\u5c14\u6ee8\u5e02","cityPinYin":"haerbinshi"},{"cityId":340100,"cityName":"\u5408\u80a5\u5e02","cityPinYin":"hefeishi"},{"cityId":150100,"cityName":"\u547c\u548c\u6d69\u7279\u5e02","cityPinYin":"huhehaoteshi"},{"cityId":431200,"cityName":"\u6000\u5316\u5e02","cityPinYin":"huaihuashi"},{"cityId":150700,"cityName":"\u547c\u4f26\u8d1d\u5c14\u5e02","cityPinYin":"hulunbeiershi"},{"cityId":420200,"cityName":"\u9ec4\u77f3\u5e02","cityPinYin":"huangshishi"},{"cityId":430400,"cityName":"\u8861\u9633\u5e02","cityPinYin":"hengyangshi"},{"cityId":330500,"cityName":"\u6e56\u5dde\u5e02","cityPinYin":"huzhoushi"},{"cityId":330100,"cityName":"\u676d\u5dde\u5e02","cityPinYin":"hangzhoushi"},{"cityId":441300,"cityName":"\u60e0\u5dde\u5e02","cityPinYin":"huizhoushi"},{"cityId":360800,"cityName":"\u5409\u5b89\u5e02","cityPinYin":"jianshi"},{"cityId":330700,"cityName":"\u91d1\u534e\u5e02","cityPinYin":"jinhuashi"},{"cityId":440700,"cityName":"\u6c5f\u95e8\u5e02","cityPinYin":"jiangmenshi"},{"cityId":230800,"cityName":"\u4f73\u6728\u65af\u5e02","cityPinYin":"jiamusishi"},{"cityId":370800,"cityName":"\u6d4e\u5b81\u5e02","cityPinYin":"jiningshi"},{"cityId":370100,"cityName":"\u6d4e\u5357\u5e02","cityPinYin":"jinanshi"},{"cityId":330400,"cityName":"\u5609\u5174\u5e02","cityPinYin":"jiaxingshi"},{"cityId":445200,"cityName":"\u63ed\u9633\u5e02","cityPinYin":"jieyangshi"},{"cityId":210700,"cityName":"\u9526\u5dde\u5e02","cityPinYin":"jinzhoushi"},{"cityId":421000,"cityName":"\u8346\u5dde\u5e02","cityPinYin":"jingzhoushi"},{"cityId":410200,"cityName":"\u5f00\u5c01\u5e02","cityPinYin":"kaifengshi"},{"cityId":530100,"cityName":"\u6606\u660e\u5e02","cityPinYin":"kunmingshi"},{"cityId":131000,"cityName":"\u5eca\u574a\u5e02","cityPinYin":"langfangshi"},{"cityId":411100,"cityName":"\u6f2f\u6cb3\u5e02","cityPinYin":"luoheshi"},{"cityId":320700,"cityName":"\u8fde\u4e91\u6e2f\u5e02","cityPinYin":"lianyungangshi"},{"cityId":350800,"cityName":"\u9f99\u5ca9\u5e02","cityPinYin":"longyanshi"},{"cityId":410300,"cityName":"\u6d1b\u9633\u5e02","cityPinYin":"luoyangshi"},{"cityId":620100,"cityName":"\u5170\u5dde\u5e02","cityPinYin":"lanzhoushi"},{"cityId":450200,"cityName":"\u67f3\u5dde\u5e02","cityPinYin":"liuzhoushi"},{"cityId":340500,"cityName":"\u9a6c\u978d\u5c71\u5e02","cityPinYin":"maanshanshi"},{"cityId":440900,"cityName":"\u8302\u540d\u5e02","cityPinYin":"maomingshi"},{"cityId":510700,"cityName":"\u7ef5\u9633\u5e02","cityPinYin":"mianyangshi"},{"cityId":330200,"cityName":"\u5b81\u6ce2\u5e02","cityPinYin":"ningboshi"},{"cityId":360100,"cityName":"\u5357\u660c\u5e02","cityPinYin":"nanchangshi"},{"cityId":350900,"cityName":"\u5b81\u5fb7\u5e02","cityPinYin":"ningdeshi"},{"cityId":511000,"cityName":"\u5185\u6c5f\u5e02","cityPinYin":"neijiangshi"},{"cityId":320100,"cityName":"\u5357\u4eac\u5e02","cityPinYin":"nanjingshi"},{"cityId":450100,"cityName":"\u5357\u5b81\u5e02","cityPinYin":"nanningshi"},{"cityId":320600,"cityName":"\u5357\u901a\u5e02","cityPinYin":"nantongshi"},{"cityId":350300,"cityName":"\u8386\u7530\u5e02","cityPinYin":"putianshi"},{"cityId":370200,"cityName":"\u9752\u5c9b\u5e02","cityPinYin":"qingdaoshi"},{"cityId":130300,"cityName":"\u79e6\u7687\u5c9b\u5e02","cityPinYin":"qinhuangdaoshi"},{"cityId":230200,"cityName":"\u9f50\u9f50\u54c8\u5c14\u5e02","cityPinYin":"qiqihaershi"},{"cityId":350500,"cityName":"\u6cc9\u5dde\u5e02","cityPinYin":"quanzhoushi"},{"cityId":231200,"cityName":"\u7ee5\u5316\u5e02","cityPinYin":"suihuashi"},{"cityId":310100,"cityName":"\u4e0a\u6d77\u5e02","cityPinYin":"shanghaishi"},{"cityId":130100,"cityName":"\u77f3\u5bb6\u5e84\u5e02","cityPinYin":"shijiazhuangshi"},{"cityId":330600,"cityName":"\u7ecd\u5174\u5e02","cityPinYin":"shaoxingshi"},{"cityId":430500,"cityName":"\u90b5\u9633\u5e02","cityPinYin":"shaoyangshi"},{"cityId":210100,"cityName":"\u6c88\u9633\u5e02","cityPinYin":"shenyangshi"},{"cityId":140600,"cityName":"\u6714\u5dde\u5e02","cityPinYin":"shuozhoushi"},{"cityId":440300,"cityName":"\u6df1\u5733\u5e02","cityPinYin":"shenzhenshi"},{"cityId":320500,"cityName":"\u82cf\u5dde\u5e02","cityPinYin":"suzhoushi"},{"cityId":370900,"cityName":"\u6cf0\u5b89\u5e02","cityPinYin":"taianshi"},{"cityId":120100,"cityName":"\u5929\u6d25\u5e02","cityPinYin":"tianjinshi"},{"cityId":340700,"cityName":"\u94dc\u9675\u5e02","cityPinYin":"tonglingshi"},{"cityId":130200,"cityName":"\u5510\u5c71\u5e02","cityPinYin":"tangshanshi"},{"cityId":140100,"cityName":"\u592a\u539f\u5e02","cityPinYin":"taiyuanshi"},{"cityId":321200,"cityName":"\u6cf0\u5dde\u5e02","cityPinYin":"taizhoushi"},{"cityId":370700,"cityName":"\u6f4d\u574a\u5e02","cityPinYin":"weifangshi"},{"cityId":420100,"cityName":"\u6b66\u6c49\u5e02","cityPinYin":"wuhanshi"},{"cityId":340200,"cityName":"\u829c\u6e56\u5e02","cityPinYin":"wuhushi"},{"cityId":610500,"cityName":"\u6e2d\u5357\u5e02","cityPinYin":"weinanshi"},{"cityId":320200,"cityName":"\u65e0\u9521\u5e02","cityPinYin":"wuxishi"},{"cityId":330300,"cityName":"\u6e29\u5dde\u5e02","cityPinYin":"wenzhoushi"},{"cityId":610100,"cityName":"\u897f\u5b89\u5e02","cityPinYin":"xianshi"},{"cityId":341800,"cityName":"\u5ba3\u57ce\u5e02","cityPinYin":"xuanchengshi"},{"cityId":411000,"cityName":"\u8bb8\u660c\u5e02","cityPinYin":"xuchangshi"},{"cityId":420900,"cityName":"\u5b5d\u611f\u5e02","cityPinYin":"xiaoganshi"},{"cityId":350200,"cityName":"\u53a6\u95e8\u5e02","cityPinYin":"xiamenshi"},{"cityId":532800,"cityName":"\u897f\u53cc\u7248\u7eb3\u50a3\u65cf\u81ea\u6cbb\u5dde","cityPinYin":"xishuangbannadaizuzizhizhou"},{"cityId":130500,"cityName":"\u90a2\u53f0\u5e02","cityPinYin":"xingtaishi"},{"cityId":430300,"cityName":"\u6e58\u6f6d\u5e02","cityPinYin":"xiangtanshi"},{"cityId":410700,"cityName":"\u65b0\u4e61\u5e02","cityPinYin":"xinxiangshi"},{"cityId":433100,"cityName":"\u6e58\u897f\u571f\u5bb6\u65cf\u82d7\u65cf\u81ea\u6cbb\u5dde","cityPinYin":"xiangxitujiazumiaozuzizhizhou"},{"cityId":360500,"cityName":"\u65b0\u4f59\u5e02","cityPinYin":"xinyushi"},{"cityId":420600,"cityName":"\u8944\u9633\u5e02","cityPinYin":"xiangyangshi"},{"cityId":320300,"cityName":"\u5f90\u5dde\u5e02","cityPinYin":"xuzhoushi"},{"cityId":511500,"cityName":"\u5b9c\u5bbe\u5e02","cityPinYin":"yibinshi"},{"cityId":640100,"cityName":"\u94f6\u5ddd\u5e02","cityPinYin":"yinchuanshi"},{"cityId":320900,"cityName":"\u76d0\u57ce\u5e02","cityPinYin":"yanchengshi"},{"cityId":420500,"cityName":"\u5b9c\u660c\u5e02","cityPinYin":"yichangshi"},{"cityId":360900,"cityName":"\u5b9c\u6625\u5e02","cityPinYin":"yichunshi"},{"cityId":210800,"cityName":"\u8425\u53e3\u5e02","cityPinYin":"yingkoushi"},{"cityId":370600,"cityName":"\u70df\u53f0\u5e02","cityPinYin":"yantaishi"},{"cityId":430600,"cityName":"\u5cb3\u9633\u5e02","cityPinYin":"yueyangshi"},{"cityId":430900,"cityName":"\u76ca\u9633\u5e02","cityPinYin":"yiyangshi"},{"cityId":321000,"cityName":"\u626c\u5dde\u5e02","cityPinYin":"yangzhoushi"},{"cityId":431100,"cityName":"\u6c38\u5dde\u5e02","cityPinYin":"yongzhoushi"},{"cityId":440400,"cityName":"\u73e0\u6d77\u5e02","cityPinYin":"zhuhaishi"},{"cityId":130700,"cityName":"\u5f20\u5bb6\u53e3\u5e02","cityPinYin":"zhangjiakoushi"},{"cityId":321100,"cityName":"\u9547\u6c5f\u5e02","cityPinYin":"zhenjiangshi"},{"cityId":440800,"cityName":"\u6e5b\u6c5f\u5e02","cityPinYin":"zhanjiangshi"},{"cityId":330900,"cityName":"\u821f\u5c71\u5e02","cityPinYin":"zhoushanshi"},{"cityId":640500,"cityName":"\u4e2d\u536b\u5e02","cityPinYin":"zhongweishi"},{"cityId":430200,"cityName":"\u682a\u6d32\u5e02","cityPinYin":"zhuzhoushi"},{"cityId":350600,"cityName":"\u6f33\u5dde\u5e02","cityPinYin":"zhangzhoushi"},{"cityId":410100,"cityName":"\u90d1\u5dde\u5e02","cityPinYin":"zhengzhoushi"}]}

js请求数据组装数据:

 var isTest = (window.location.href.indexOf(‘localhost‘) >0 || window.location.href.indexOf(‘127.0.0.1‘) >0);
    var url = isTest ? ‘./data/city.js‘ : ‘真实url‘;
    var cityMap={};
    $.ajax({
        type:"Get",
        url:url,
        timeout:5000,
        data:"",
        success:function(result){
            if(isTest){
                result=JSON.parse(result);
            }
            //alert(result.data);
           if(result.data && result.data.length>0){
                   $.each(result.data,function(i,item){
                       var firstChar=item.cityPinYin.charAt(0).toUpperCase();
                       cityMap[firstChar]=cityMap[firstChar]?cityMap[firstChar]:[];
                       cityMap[firstChar].push({
                           cityId:item.cityId,
                           cityName:item.cityName
                       });
                   });

                //获得模板
                var lettTemp=_.template($("#letterList").html());
                var cityTemp=_.template($("#cityList").html());

               //渲染模板
              $("#fixedNav").html(lettTemp({"data":cityMap}));
               $("#contentList").append(cityTemp({"data":cityMap}));

                // $.each(cityMap,function(key,item){
                //    console.log("key="+key);
                //    $.each(item,function(i,city){
                //       console.log("cityName="+city.cityName);
                //    })
                // });
                // console.log(cityMap);

           }
        }

    })

模板:

<script type="text/template" id="letterList">
      <a href="javascript:;" style="line-height: 27.2727px;" class="">*</a>
      <%_.each(data,function(value,index,obj){%>
          <a href="javascript:void(0)" class="ng-binding ng-scope" style="line-height: 27.2727px;" data-id="<%=index%>"><%=index%></a>
      <%})%>
</script>

<script type="text/template" id="cityList">
      <%_.each(data,function(cityList,index){%>
          <div class="ng-scope">
              <dt class="ng-binding" id="<%=index%>">
                   <%=index%>
              </dt>
              <%_.each(cityList,function(item,index){%>
                    <dd  class="ng-binding ng-scope" data-id="<%=item.cityId%>"><%=item.cityName%></dd>
             <%})%>
           </div>
      <%})%>
</script>

备注:

例如如上模板渲染中的_.each方法,此方法中第一个参数为value值,第二个参数为index值,区别与jquery中的$.each方法

具体此方法的用法请参考官方文档

以及:http://blog.itrunc.com/2014/04/29/underscore-each/

时间: 2024-10-17 07:50:22

underscore中template的使用Demo的相关文章

理解Underscore中的_.template函数

Underscore中提供了_.template函数实现模板引擎功能,它可以将JSON数据源中的数据对应的填充到提供的字符串中去,类似于服务端渲染的模板引擎.接下来看一下Underscore是如何实现模板引擎的. 工具准备 首先是_.template函数的配置项,Underscore源码中配置了默认的配置项: _.templateSettings = { // 执行JavaScript语句,并将结果插入. evaluate: /<%([\s\S]+?)%>/g, // 插入变量的值. inte

关于 underscore 中模板引擎的应用演示样例

//关于 underscore 中模板引擎的应用演示样例 <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>关于 underscore

HTML5中&lt;template&gt;标签的详细介绍

HTML5中<template>标签的详细介绍(图文) 这篇文章主要介绍了HTML5中的template标签,是HTML5入门中的重要知识,需要的朋友可以参考 一.HTML5 template元素初面 <template>元素,基本上可以确定是2013年才出现的.干嘛用的呢,顾名思意,就是用来声明是“模板元素”. 目前,我们在HTML中嵌入模板HTML,往往是类似这样的写法: 1 2 3 <script type="text/template"> /

C++中template的简单用法

模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计.C++ 的标准库提供许多有用的函数大多结合了模板的观念,如STL以及IO Stream.使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数.返回值取得任意类型. 一.函数模板 在c++入门中,很多人会接触swap(int&, int&)这样的函数类似代码如下: 1 void swap(int&a , int& b) { 2 int temp

Android中Service的一个Demo例子

Android中Service的一个Demo例子  Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding.  本文,主要贴代码,不对Service做过多讲解.  代码是从网上找的一个例子,Copy下来发现代码不完全正确,稍微修改了下.  AndroidManifest.xml <application android:icon="@drawable/ic_launcher" android:label="@stri

OpenStack-Heat中template的formatversion介绍

template中有三个formatversion,第一种是aws的格式,表示这是一个json文件:第二种是heat为了扩展aws并且更好的加入注释,引入的yaml格式文件:第三种是openstack的heat特有的hot格式的头部,也是基于yaml格式的. "AWSTemplateFormatVersion": "2010-09-09", HeatTemplateFormatVersion: '2012-12-12' heat_template_version:

backbone和underscore中的extend

总是把这两个库中的extend搞混了所以写下来. backbone中的extend实现了继承: 1 // Helper function to correctly set up the prototype chain for subclasses. 2 // Similar to `goog.inherits`, but uses a hash of prototype properties and 3 // class properties to be extended. 4 var exte

横冲直撞vue(第三篇):vue中template的三种写法、v-bind、v-on、更新元素的指令v-text与v-html、条件渲染指令v-if 与v-show、v-for

一.vue中template的三种写法 第一种(字符串模板写法): 直接写在vue构造器里,这种写法比较直观,适用于html代码不多的场景,但是如果模板里html代码太多,不便于维护,不建议这么写. <body> <div id="app"></div> <script src="./lib/vue.js"></script> <script type="text/javascript&qu

Winform开发框架中的综合案例Demo

在实际的系统开发中,我们往往需要一些简单的的案例代码,基于此目的我把Winform开发框架中各种闪光点和不错的功能,有些是我们对功能模块的简单封装,而有些则是引入了一些应用广泛的开源组件进行集成使用,因此把它们做了一个Demo进行展示,以方便我们随时了解和参考,并能够快速应用相应的场景到具体的项目中. 1.闪屏展示界面及主体界面 在很多系统里面,提供一个设计不错的图片作为程序界面展示的效果是挺不错的,这个小程序也不例外,基本上整合了一些WInform界面常用的各种功能. 而整个案例的界面的界面以