For loop in Hogan JS template

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I am using Express JS and Hogan JS template engine. I know hogan is logic less template but I need to execute a for loop in view code to generate table fields.

I have done lots of googling but I did not found any solution. I know how to do if-else in Hogan JS.

I read all the documentation in Hogan JS and Mustache JS websites.

I am getting values in the json format.

[     {         "email": "abc@example.com",         "name": "abc",         "date": "05/01/2015"     },     {         "email": "xyz@example.com",         "name": "xyz",         "date": "05/01/2015"     } ] 

this is sample json, there may be any amount of data. To show this data in table in view I need to iterate a loop. So I need a code for for-loop.

回答1:

You can certainly do that.

Assign the data into a nested JSON object and them compile template for parent key.

var data = {"list" : [    {        "email": "abc@example.com",        "name": "abc",        "date": "05/01/2015"    },    {        "email": "xyz@example.com",        "name": "xyz",        "date": "05/01/2015"    } ]};   var template = Hogan.compile("{{#list}} Your name is {{name}} and email is {{email}} <br/>{{/list}}");  var output = template.render(data);  

Here is the working example



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!