6

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?

2 Answers 2

11

Try 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 }}
1
  • works perfect, thanks! i forgot to put :index in {{~it.books :value:index}}
    – flygge
    Commented Dec 6, 2013 at 8:44
0

take a look a case somewhat more complex

{
    "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.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": "[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
                                                    .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]]
                    }
    {{ 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.