对于Hogan JS模板中的循环 [英] For loop in Hogan JS template

查看:370
本文介绍了对于Hogan JS模板中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Express JS和Hogan JS模板引擎。我知道hogan是逻辑较少的模板,但是我需要在视图代码中执行for循环来生成表字段。



我已经做了很多谷歌搜索,但没有找到任何解决方案。我知道如何在Hogan JS中执行 if-else



我阅读了胡子JS 网站。



我以json格式获取价值。

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

这是json样本,可能会有任何数据量。为了在表中显示此数据,我需要迭代一个循环。
所以我需要一个for循环的代码。

解决方案

你当然可以这样做。



将数据分配到嵌套的JSON对象中,并为父键编译模板。

 code> 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}}您的姓名是{{name}},电子邮件是{{email}}< br /> {{/ list}})
var output = template.render(data);

这是工作的示例


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.

解决方案

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

这篇关于对于Hogan JS模板中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆