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

2024-04-12

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

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

...但我明白了:

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 to JSON.parse( item )上面,我认为没有错误而是这样:

function parse() { [native code] }

这是itemJSON 对象:

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

简而言之,我想访问各个键和值item在 Swig 中,但我无法让 JSON.parse() 工作。如果有人能给我指出正确的方向,我将不胜感激(我听说 Swig 基于 Django 模板,所以如果您使用过这些模板,这应该很容易)。


我不知道你为什么要解析它。在 swig 中它已经是 JSON 了。 所以要获得标题,请更改{{ JSON.parse(item).title }} to {{ item.title }} .

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swig (Node.js) 中的 JSON.parse() ? 的相关文章

随机推荐