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

查看:148
本文介绍了Swig(Node.js)中的JSON.parse()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我被困在这里时,我正试图从Jade切换到Swig(受Swig疯狂的性能困扰),作为我的Express模板引擎—我正在从Express发送一系列序列化JSON到Swig,并在Swig中检索数据在这里使用此循环:

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:

<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)

如果我将JSON.parse( item ).title更改为上面的JSON.parse( item ),则我看到的不是错误,而是错误:

If I change JSON.parse( item ).title to JSON.parse( item ) above, instead of errors I get this in my view:

function parse() { [native code] }

下面是item JSON对象:

Here's a look at the item JSON object:

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

简单地说,我想访问Swig中的item的各个键和值,但是我无法使JSON.parse()正常工作.如果有人会向我指出正确的方向,我将不胜感激(我被告知Swig基于Django模板,因此,如果您使用过这些模板,这应该很容易).

Simply put, I want to access the individual keys and values of item in Swig but I can't get JSON.parse() to work. 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).

推荐答案

我不知道为什么要解析它.总而言之,它已经是JSON. 因此,要获取标题,请将{{ JSON.parse(item).title }}更改为{{ item.title }}.

I don't know why you want to parse it. In swig it already is JSON. So to get the title, change {{ JSON.parse(item).title }} to {{ item.title }} .

这篇关于Swig(Node.js)中的JSON.parse()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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