mustache / mustache.github.com

The {{official}} website
http://mustache.github.io/
Other
2.29k stars 292 forks source link

How to escape a partial's output? #118

Closed mesztam closed 8 months ago

mesztam commented 5 years ago

Hi, is there any way to escape the output of a partial? E.g. when using the output of the partial as the value of an attribute like this: <input type="hidden" value="{{> generateHtml }} "/> Thanks in advance!

leonardopsantos commented 4 years ago

Can you elaborate on your question with an example of what you want as output and some code?

I tested this with pystache and partials are escaped:

context =\
{
    'users' : [
        {'name': 'John',
         'job' : 'Engineer'},
        {'name': 'Leo',
         'job' : 'Coder<br>'}
    ]
}

full_template =\
'''
{{#users}}
{{name}} is an {{job}}
{{/users}} 
'''

renderer = pystache.Renderer()

output = renderer.render(full_template, context)
print(output)

The above produces:

John is an Engineer
Leo is an Coder&lt;br&gt;

With partials:

base_template =\
'''
{{#users}}
{{> user}}
{{/users}} 
'''

print("The same data now as a partial with a path:\n")

output = renderer.render(base_template, context)
print(output)

user.mustache:

{{name}} is an {{job}}

Produces:

John is an Engineer
Leo is an Coder&lt;br&gt;
jgonggrijp commented 8 months ago

The spec does not state whether partials should be escaped or not. Some implementations do, some don't. Some might have a triple-mustache variant of every tag type so each can be either escaped or not escaped.

If you believe that something should be standardized, you can take it to https://github.com/mustache/spec/discussions.

Closing this, as this repository is only about the Mustache website, not about the template language.