Mustachio: if-statement without scoping - is it possible?
Asked Answered
I

1

7

I'm moving e-mail templates from Mandrill to Postmark which requires converting Handlebars to Mustachio. In Handlebars I had something like this:

{{#if some_variable}}
<p>This text uses variable: {{some_variable}}
{{/if}}

According to Mustache documentation it should look like this after converting:

{{#some_variable}}
<p>This text uses variable: {{some_variable}}
{{/some_variable}}

The problem is that Postmark's Mustachio uses scoping (https://github.com/wildbit/mustachio/wiki#scoping), so in that case it expects following JSON model:

{
    "some_variable": {
        "some_variable": "some_variable_value"
    }
}

instead of

{
    "some_variable": "some_variable_value"
}

Does anyone know how to turn off Mustachio's scoping, so it uses expected exemplary JSON model? The only workaround (dirty one) I see so far is to pass template model in this nested object form, but I already found out that it wont work in all cases. Thanks in advance, any help appreciated.

Ilana answered 27/4, 2016 at 8:15 Comment(0)
I
19

Ok, found an anwser to that problem. According to the docs https://github.com/wildbit/mustachio/wiki#inverted-groups-or-how-to-make-placeholders what I should do in that case is:

{{#some_variable}}
<p>This text uses variable: {{.}}</p>
{{/some_variable}}

Then sending JSON model like:

{
    "some_variable": "some_variable_value"
}

will result in

<p>This text uses variable: some_variable_value</p>

So the answer to the problem is to use {{.}} operator that points to the tag value.

Ilana answered 27/4, 2016 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.