Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustachio: if-statement without scoping - is it possible?

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.

like image 289
Szymon Stepniak Avatar asked Dec 24 '22 06:12

Szymon Stepniak


1 Answers

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.

like image 128
Szymon Stepniak Avatar answered Feb 11 '23 15:02

Szymon Stepniak