码迷,mamicode.com
首页 > Web开发 > 详细

jsRender 循环for 和props

时间:2015-06-13 15:38:01      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 

jsrender提供多重循环方式

1.{{for array}}循环数组

2.{{props object}}循环对象

 

 

1.for array的使用

<body>

<div id="result"></div>

<script id="theTmpl" type="text/x-jsrender">
<b>{{:title}}</b>
<ul>
  {{for members}}
      <li>{{:name}} lives in <b>{{:address.city}}</b></li>
  {{/for}}
</ul>
</script>

<script>
var data = {
  "title": "The A team",
  "members": [
    {
      "name": "Pete",
      "address": {
        "city": "Seattle"
      }
    },
    {
      "name": "Heidi",
      "address": {
        "city": "Sidney"
      }
    }
  ]
};

var template = $.templates("#theTmpl");

var htmlOutput = template.render(data);

$("#result").html(htmlOutput);
</script>

</body>

2.props object的使用

循环object中的所有属性

<body>

<div id="result"></div>

<script id="theTmpl" type="text/x-jsrender">
<table><tbody>
  <tr><td><b>name:</b> {{:name}}</td></tr>
  <tr><td> 
  {{props address}}
    <b>{{>key}}:</b> {{>prop}}<br/>
  {{/props}}
  </td></tr>
</tbody></table>
</script>

<script>
var data = [
  {
    "name": "Pete",
    "address": {
      "street": "12 Pike Place",
      "city": "Seattle",
      "ZIP": "98101"
    }
  },
  {
    "name": "Heidi",
    "address": {
      "street": "5000 Broadway",
      "city": "Sidney",
      "country": "Australia"
    }
  }
];

var template = $.templates("#theTmpl");

var htmlOutput = template.render(data);

$("#result").html(htmlOutput);
</script>

</body>

 

jsRender 循环for 和props

标签:

原文地址:http://www.cnblogs.com/hwaggLee/p/4573565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!