twitter / hogan.js

A compiler for the Mustache templating language
http://twitter.github.io/hogan.js
Apache License 2.0
5.14k stars 431 forks source link

How to embed json variables with in a script tag as a variable #240

Closed sijo-vijayan closed 8 years ago

sijo-vijayan commented 8 years ago

I would like to embed http post variable into a javascript local variable in the .hjs template file. I have done this like given below.

In the route handler

app.post('/thankyou', function(req, res, next) {
    res.render('index',{ req: JSON.stringify(req.body) });
});

In the index.hjs file

<script>
var postvar = {{req}}
</script>

But json value printing in the final html like the following

<script>
var postvar = {&quot;username&quot;:&quot;sijo@example.com&quot;}
</script>

Expected output is given below

<script>
var postvar = {'username':'sijo@example.com'}
</script>

Please give me a solution.

ex1st commented 8 years ago

var postvar = {{{req}}}

sijo-vijayan commented 8 years ago

Now it working fine, I can't find this triple curly brackets anywhere on its readme file. Where i can find the reference of this triple brackets ?

Thanks for your help.

ex1st commented 8 years ago

http://mustache.github.io/mustache.5.html

All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.

charliejlryan commented 8 years ago

Hogan uses Mustache - {{{var}}} is the syntax for outputting unescaped HTML, and can be found on the Mustache GitHub as referenced by the Hogan readme https://mustache.github.io/mustache.5.html

On 8 Feb 2016, at 09:11, sijo-vijayan notifications@github.com wrote:

Now it working fine, I can't find this triple curly brackets anywhere on its readme file. Where i can find the reference of this triple brackets ?

Thanks for your help.

— Reply to this email directly or view it on GitHub.

sijo-vijayan commented 8 years ago

Thanks for you help @ex1st @charliejlryan