admin管理员组文章数量:1291108
I have a doT.js template looking like this:
{{?it.books.length }}
{{~it.books :value}}
<li>
<article class='Teaser'>
<a href='{{=value.url}}' title='{{=value.title}}'>
<img src='{{=value.image}}' />
</a>
<h3>
<a href='{{=value.url}}' title='{{=value.title}}'>{{=value.title}}</a>
</h3>
</article>
</li>
// this should only be rendered every 3rd time
<br class='clear' />
{{~}}
{{?}}
The br-Tag in the end should only be rendered every third time. How do I do this?
I have a doT.js template looking like this:
{{?it.books.length }}
{{~it.books :value}}
<li>
<article class='Teaser'>
<a href='{{=value.url}}' title='{{=value.title}}'>
<img src='{{=value.image}}' />
</a>
<h3>
<a href='{{=value.url}}' title='{{=value.title}}'>{{=value.title}}</a>
</h3>
</article>
</li>
// this should only be rendered every 3rd time
<br class='clear' />
{{~}}
{{?}}
The br-Tag in the end should only be rendered every third time. How do I do this?
Share Improve this question edited Nov 16, 2014 at 1:09 JstnPwll 8,6952 gold badges35 silver badges57 bronze badges asked Nov 26, 2013 at 10:46 flyggeflygge 1892 silver badges11 bronze badges2 Answers
Reset to default 11Try this:
{{?it.books.length }}
{{~it.books :value:index}}
<li>
<article class='Teaser'>
<a href='{{=value.url}}' title='{{=value.title}}'>
<img src='{{=value.image}}' />
</a>
<h3>
<a href='{{=value.url}}'
title='{{=value.title}}'>{{=value.title}}</a>
</h3>
</article>
</li>
{{? index % 3 == 2 }}
<br class='clear' />
{{?}}
{{~}}
{{?}}
If you want to avoid adding the element at the end of the list (which occurs when the collection's length is divisible by 3), replace
{{? index%3 == 2 }}
with
{{? it.books.length-1 != index && index % 3 == 2 }}
take a look a case somewhat more plex
{
"result": [
["Administrators", 1, [{
"id": 189,
"role_id": 1,
"user": {
"id": 8,
"username": "[email protected]",
"email": "[email protected]",
"firstName": "Agnaldo",
"lastName": "Carmo",
"created": {
"date": "2015-09-25
18: 56: 25.000000 ","
timezone_type ":3,"
timezone ":"
America\ / Sao_Paulo "},"
modified ":{"
date ":"
2015 - 09 - 25 18: 56: 25.000000 ","
timezone_type ":3,"
timezone ":"
America\ / Sao_Paulo "},"
relativePath ":"
ws\ / uploads\ / User\ / 8\ / 0278845942. jpg ","
absolutePath ":"
ws\ / uploads\ / User\ / 8\ / 0278845942. jpg ","
role ":[1],"
language_id ":3
}
}]],
["Writers", 2, [{
"id": 135,
"role_id": 2,
"user": {
"id": 7,
"username": "camilaguimaraes",
"email": "camilag
@gmail. ","
firstName ":"
Camila ","
lastName ":"
Guimaraes ","
created ":{"
date ":" - 0001 - 11 - 30 00: 00: 00.000000 ",
"timezone_type": 3,
"timezone": "America\/Sao_Paulo"
},
"modified": {
"date": "-0001-11-30 00:00:00.000000",
"timezone_type": 3,
"timezone": "America\/Sao_Paulo"
},
"relativePath": null,
"absolutePath": null,
"role": [2],
"language_id": 1
}
}, {
"id": 183,
"role_id": 2,
"user": {
"id": 20,
"username": "[email protected]",
"email": "[email protected]",
"firstName": "Agnaldo",
"lastName": "Carmo",
"created": {
"date": "2015-11-23 10:41: 34.000000 ","
timezone_type ":3,"
timezone ":"
America\ / Sao_Paulo "},"
modified ":{"
date ":"
2015 - 11 - 23 10: 41: 34
.000000 ","
timezone_type ":3,"
timezone ":"
America\ / Sao_Paulo "},"
relativePath ":null,"
absolutePath ":null,"
role ": [2, 3, 4],
"language_id": 1
}
},
{
"id": 186,
"role_id": 2,
"user": {
"id": 2,
"username": "[email protected]",
"email": "[email protected]",
"firstName": "zarko",
"lastName": "krneta",
"created": {
"date": "-0001-11-30 00:00:00.000000",
"timezone_type": 3,
"timezone": "America\/Sao_Paulo"
},
"modified": {
"date": "2015-06-10 15:51:30.000000",
"timezone_type": 3,
"timezone": "America\/Sao_Paulo"
},
"relativePath": "ws\/uploads\/User\/2\/1327788642.jpg",
"absolutePath": "ws\/uploads\/User\/2\/1327788642.jpg",
"role": [2],
"language_id": 1
}
}]],
["Editors", 3, [{
"id": 184,
"role_id": 3,
"user": {
"id": 20,
"username": "[email protected]",
"email": "[email protected]",
"firstName": "Agnaldo",
"lastName": "Carmo",
"created": {
"date": "2015-11-23 10:41:34.000000",
"timezone_type": 3,
"timezone": "America\ / Sao_Paulo "},"
modified ":{"
date ":"
2015 - 11 - 23 10: 41: 34.000000 ","
timezone_type ":3,"
timezone ":"
America\ / Sao_Paulo "},"
relativePath ":null,"
absolutePath ":null,"
role ":[2,3,4],"
language_id ":1}}]],["
Auditors ",4,
[{
"id": 185,
"role_id": 4,
"user": {
"id": 20,
"username": "[email protected]",
"email": "agnaldo12@hotmail
. ","
firstName ":"
Agnaldo ","
lastName ":"
Carmo ","
created ":{"
date ":"
2015 - 11 - 23 10: 41: 34.000000 ","
timezone_type ": 3,
"timezone": "America\/Sao_Paulo"
},
"modified": {
"date": "2015-11-23 10:41:34.000000",
"timezone_type": 3,
"timezone": "America\/Sao_Paulo"
},
"relativePath": null,
"absolutePath": null,
"role": [2, 3, 4],
"language_id": 1
}
}]],
["teste", 6, null],
["teste1", 7, null],
["teste2", 8, null]]
}
{{ for(var prop in it.userCollection) { }}
<!-- Prints {{=prop}} "msg" -->
</br>
<div> {{=it.userCollection[prop]['attributes'][0]}}</div>
</br>
{{ for(var numProp in it.userCollection[prop]['attributes'][2]) { }}
{{
var VRoleId = it.userCollection[prop]['attributes'][2][numProp]['role_id'];
var user = it.userCollection[prop]['attributes'][2][numProp]['user'];
}}
<!-- Prints "a: a1" and "b: b1" -->
<div>Role id {{=VRoleId }}</div>
<div>User Name{{=user['username'] }}</div>
{{ } }}
{{ } }}
its result
Administrators
Role id 1 User [email protected]
Writers
Role id 2 User Namecamilaguimaraes Role id 2 User [email protected] Role id 2 User [email protected]
Editors
Role id 3 User [email protected]
Auditors
Role id 4 User [email protected]
teste
teste1
teste2
本文标签: javascriptdoTjs do something every 3rd iterationStack Overflow
版权声明:本文标题:javascript - doT.js do something every 3rd iteration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741501652a2382083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论