使用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>
我如何为Feature 2等做类似的事情?
答案 0 :(得分: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 %}
此外,作为一般经验法则,人们普遍认为使用点符号可访问的键更快更容易(不需要引号)。