Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to escape {{}} in javascript handlebars?

I have the following ‘my.txt’ file,

Input file

name={{applicationName}}
env=app/{{env}}/{{applicationName}}

I wish to update my text file using javascript handlebars, in such a way that my text file gets updated as,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected output

name=myFirstApp
env=app/{{env}}/myFirstApp

I am trying to escape the character "{{env}}", but unfortunately it’s not working as expected and instead I’m getting below output,

My output

name=myFirstApp
env=myFirstApp//app

My code looks like below,

readContent = fs.readFileSync(`${__dirname}/../my.txt`, 'utf8')
handlebarsTemplate = Handlebars.compile(readContent)
const output = handlebarsTemplate({
  applicationName: 'myFirstApp'
})
fs.writeFileSync(`newFile.txt`, output, 'utf8')

I am new to this concept. Any help would be appreciated. Thanks!

>Solution :

https://handlebarsjs.com/guide/expressions.html#escaping-handlebars-expressions

Handlebars content may be escaped in one of two ways, inline escapes or raw block helpers. Inline escapes created by prefixing a mustache block with \. Raw blocks are created using {{{{ mustache braces.

\{{escaped}}
{{{{raw}}}}
  {{escaped}}
{{{{/raw}}}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading