How to change the default delimiter of mustache.js?

by elise_daugherty , in category: Javascript , 16 days ago

How to change the default delimiter of mustache.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 15 days ago

@elise_daugherty 

If you want to change the default delimiter of mustache.js, you can use the Mustache.tags object to set new tag delimiters. Here's how you can do it:

  1. Set the new tag delimiters:
1
Mustache.tags = ['[[', ']]'];


  1. Use the new delimiters in your template:
1
2
3
<script id="template" type="x-tmpl-mustache">
    Hello, [[name]]!
</script>


  1. Render the template with the new delimiters:
1
2
3
4
var template = document.getElementById('template').innerHTML;
var data = { name: 'John' };
var rendered = Mustache.render(template, data);
console.log(rendered); // Output: Hello, John!


By following these steps, you can change the default delimiter of mustache.js to your preferred characters.