简体   繁体   English

Swig(Node.js)中的JSON.parse()?

[英]JSON.parse() in Swig (Node.js)?

I'm trying to switch from Jade to Swig (lured by Swig's insane performance) as my Express template engine when I got stuck here — I'm sending an array of serialized JSON from Express into Swig and retrieve the data in Swig using this loop here: 当我被困在这里时,我正尝试从Jade切换到Swig(受Swig疯狂的性能困扰)作为Express模板引擎-我正在将Expressed的序列化JSON数组发送到Swig,并使用此循环在Swig中检索数据这里:

<ul id = "list">
    {% if items %}
        {% for item in items %}
            {{ JSON.parse( item ).title }}
        {% endfor %}
    {% endif %}
</ul>

... but I get this: ...但是我明白了:

SyntaxError: Unexpected token )
    at Object.Function (unknown source)
    at createTemplate (/home/vijay/node_modules/swig/index.js:72:14)
    at getTemplate (/home/vijay/node_modules/swig/index.js:109:26)
    at Object.compile (/home/vijay/node_modules/swig/index.js:153:16)

If I change JSON.parse( item ).title to JSON.parse( item ) above, instead of errors I get this in my view: 如果我将JSON.parse( item ).title为上述JSON.parse( item ) ,则不会出现错误,而是在我的视图中看到了这一点:

function parse() { [native code] }

Here's a look at the item JSON object: 看一下item JSON对象:

item { 
    id     : 2,
    title  : 'City Life ',
    author : 'Timothy J. Lindenburg',
    date   : 1337498792626,
    indent : 0         
}

Simply put, I want to access the individual keys and values of item in Swig but I can't get JSON.parse() to work. 简而言之,我想访问Swig中item的各个键和值,但无法使JSON.parse()正常工作。 Would appreciate it if someone would point me in the right direction (I'm told Swig is based on Django templates so if you've worked with those, this should be easy). 如果有人能向我指出正确的方向,我将不胜感激(我被告知Swig基于Django模板,因此如果您使用过这些模板,这应该很容易)。

I don't know why you want to parse it. 我不知道您为什么要解析它。 In swig it already is JSON. 总而言之,它已经是JSON。 So to get the title, change {{ JSON.parse(item).title }} to {{ item.title }} . 因此,要获取标题,请将{{ JSON.parse(item).title }}更改为{{ item.title }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM