Question

When using Swig (v.1.2.2) I am having trouble trying to use an object with keys that contain a white space.

For instance, how do I use the following object in a Swig template?

{ _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' }

So, for all of the keys without whitespace, i can use them easily with something like:

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

How do I do something similar for Feature 2, etc?

Was it helpful?

Solution

This is not supported unless you nest those items one-level deep:

{
    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',
    }
}

Then you can access like this:

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

Or loop:

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

Also, as a general rule-of-thumb, it's widely considered faster and easier to use keys that are dot-notation-accessible (don't require quotes).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top