0

I'm trying to make Mustache JS output content without parsing some variables. For example:

{{block.type}}-{{block.id}}-label-{{element.id}}

I want it to just parse the block, and that is why I am giving it the following JSON:

{ block: { type: 'news', id: 23 } }

The end result should be

news-23-label-{{element.id}}

but instead it is

news-23-label-

How should I make it not parse a part of the code? I'm new in Mustache JS and I could not find this in the documentation (comments I understood, if and foreach I understood, but I could not find this).

2 Answers 2

2

Would this workaround help you? Click for fiddle.

HTML:

<div id="output"></div>

<script type="text/html" id="test1">
    {{block.type}}-{{block.id}}-label-{{block.elId}}
</script>

JS:

var output = $("#output"),
    template = $("#test1").html(),
    data = '{ "block": { "type": "news", "id": 23, "elId": "{{element.id}}" } }',
    html = Mustache.render(template, JSON.parse(data));

output.append(html);

Output:

news-23-label-{{element.id}}
Sign up to request clarification or add additional context in comments.

1 Comment

It's a good workaround, but would be awesome to just have it from the start
0

Seems to be possible now. You can change and then restore the default delimiter. Example from documentation's Variables section you can find here https://github.com/janl/mustache.js (see last three lines of template):

View:

{
  "name": "Chris",
  "company": "<b>GitHub</b>"
}

Template:

* {{name}}
* {{age}}
* {{company}}
* {{{company}}}
* {{&company}}
{{=<% %>=}}
* {{company}}
<%={{ }}=%>

Output:

* Chris
*
* &lt;b&gt;GitHub&lt;/b&gt;
* <b>GitHub</b>
* <b>GitHub</b>
* {{company}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.