doT.js每隔3次迭代做一些事情

时间:2013-11-26 10:46:45

标签: javascript json dot.js

我有一个看起来像这样的doT.js模板:

{{?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' />
{{~}}
{{?}}

最后应该每隔三次渲染一次br-Tag。我该怎么做?

2 个答案:

答案 0 :(得分:10)

试试这个:

{{?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' />
{{?}}
{{~}}
{{?}}

如果你想避免在列表的末尾添加元素(当集合的长度可被3整除时发生),替换

{{? index%3 == 2 }}

{{? it.books.length-1 != index && index % 3 == 2 }}

答案 1 :(得分:0)

看一个更复杂的案例

&#13;
&#13;
{
    "result": [
            ["Administrators", 1, [{
                    "id": 189,
                    "role_id": 1,
                    "user": {
                        "id": 8,
                        "username": "agnaldocarmo@gmail.com",
                        "email": "agnaldocarmo@gmail.com",
                        "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.com ","
                            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": "agnaldo12@hotmail.com",
                        "email": "agnaldo12@hotmail.com",
                        "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": "zarkok@gmail.com",
                            "email": "zarkok@gmail.com",
                            "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": "agnaldo12@hotmail.com",
                                "email": "agnaldo12@hotmail.com",
                                "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": "agnaldo12@hotmail.com",
                                                "email": "agnaldo12@hotmail
                                                    .com ","
                                                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]]
                    }
&#13;
    {{ 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>
        {{ } }}
    {{ } }}
&#13;
&#13;
&#13;

结果

管理员

角色ID 1 用户名agnaldocarmo@gmail.com

作家

角色ID 2 用户名CAMilaguimaraes 角色ID 2 用户名agnaldo12@hotmail.com 角色ID 2 用户名zarkok@gmail.com

编辑

角色ID 3 用户名agnaldo12@hotmail.com

审计

角色ID 4 用户名agnaldo12@hotmail.com

睾丸中

teste1

teste2