0

使用 Swig (v.1.2.2) 时,我无法尝试使用带有包含空格的键的对象。

例如,如何在 Swig 模板中使用以下对象?

{ _lang: 'english',
Title: 'This is the title',
Sub_title: 'This is the sub title',
Feature_1: 'This is the feature 1',
'Feature 2': 'This is the feature 2',
'Feature 3': 'This is the feature 3',
'Feature 4': 'This is the feature 4',
'Feature 5': 'This is the feature 5',
'Feature 6': 'This is the feature 6',
Footer: 'This is the footer' }

因此,对于所有没有空格的键,我可以轻松地使用它们,例如:

<p> {{Feature_1}}</p>

我如何为功能 2 等做类似的事情?

4

1 回答 1

0

除非您将这些项目嵌套一层深,否则不支持此功能:

{
    Title: 'This is the title',
    Features: {
        'Feature 1': 'This is the feature 1',
        'Feature 2': 'This is the feature 2',
        'Feature 3': 'This is the feature 3',
        'Feature 4': 'This is the feature 4',
        'Feature 5': 'This is the feature 5',
        'Feature 6': 'This is the feature 6',
    }
}

然后你可以像这样访问:

<p>{{ Features['Feature 1'] }}</p>

或循环:

{% for key,feature in Features %}
    <p>{{ feature }}</p>
{% endfor %}

此外,作为一般经验法则,人们普遍认为使用点符号可访问的键(不需要引号)更快、更容易。

于 2013-12-19T17:15:15.397 回答