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

これらの項目を 1 レベル深くネストしない限り、これはサポートされません。

{
    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 に答える