End-to-end dot.js

With the dot.js template engine, how do you go through an object? In the examples below, how do you loop through an msg object?

{ "msg": { "1": { "a": "a1" }, "2": { "b": "b2" } } } 
+5
source share
2 answers

From the example on the website , it looks like you should do this:

 {{ for(var prop in it) { }} <div>{{=prop}}</div> <!-- Prints "msg" --> {{ for(var msgProp in it[prop]) { }} <div>{{=msgProp}}</div> <!-- Prints "1" and "2" --> {{ for(var numProp in it[prop][msgProp]) { }} <!-- Prints "a: a1" and "b: b1" --> <div>{{=prop}}: {{=it[prop][msgProp][numProp]}}</div> {{ } }} {{ } }} {{ } }} 

However, you can simplify this object a bit with Javascript before passing it to the template to simplify the iteration.

+6
source

It would be best to convert msg to an array first. It is much easier to repeat like this. After that, just use jQuery $.each() .

0
source

All Articles