Search code examples
meteorspacebars

How to manipulate Spacebars when an object property uses colon in its writing?


I'm trying to learn how to work with meteor and google analytics API.

I had a problem with the syntax of the spacebars...

When I try to access an object everything works well...like this:

HTML - Using space bars double brackets to access an atribute of an object inside another object:

    {{# each performanceRow}}
            <tr>
                <td class="choiceId {{selectedChoice}}">{{name}}</td>
                <td class="choiceId {{selectedChoice}}">{{indicator}}</td>
                <td class="choiceId {{selectedChoice}}">{{goal}}</td>
                <td class="choiceId {{selectedChoice}}">             {{current.profileInfo.profileId}}</td>
            </tr>
   {{/each}}

The problem happens because one of the attributes is written as follow: ga:exitRate

And then, if I try to reproduce it with space-bars meteor doesn't recognize the colon as the name of the field. As follows:

{{current.totalsForAllResults.ga:exitRate}}

How can I contour this situation?


Solution

  • From the guide: A Spacebars identifier is either a JavaScript identifier name or any string enclosed in square brackets ([ what a nice string ]). As such, I presume you can just use it like this:

    {{ [current.totalsForAllResults.ga:exitRate] }}
    

    I haven't tried with nested objects yet, but I know for sure that it works with identifiers that have a space in it, so e.g. this works

    {{ [custom field] }}
    

    when you would access it from javascript like this

    doc['custom field']
    

    Let me know it works.