0
votes

I'm using Laravel and all my templates are using Blade templating. There are a few parts of pages which need to be rendered both on the server side and by JavaScript. For these I'm using Mustache templates (with mustache-l4).

The problem is how to have Laravel render the templates, but also to include them in the page with the Mustache tags intact, for the JS to pick up.

For example, if I have a (simplified example) Blade template like this:

<html>
<head></head>
<body>
    <h1>{{ $pageTitle }}</h1>
    @include('partials/'.$partialName, array('some_text' => $pageText))

    <script type="text/mustache">
        @include('partials/'.$partialName)
    </script>
</body>
</html>

which includes .mustache partials like this:

{{#some_text}}
    <p>{{some_text}}</p>
{{/some_text}}

The Mustache partial is rendered on page load fine. BUT when it's included between the <script></script> tags I want it to be rendered verbatim. ie, the {{#some_text}} Mustache tags should be there in the rendered HTML. Then the JavaScript can read it in as a Mustache template.

I can see this might be possible by changing the tag delimiters on the server-side (Blade) templates but at this stage, with scores of Blade templates already working, I'd rather not.

I can't get my head round how to do this. Could I change the Mustache delimiters just for one of the times it's included, so it's only fully rendered then?

2

2 Answers

7
votes

Another way would be to escape the tags with @

<div>@{{ $str }}</div>
<div>@{{"<a>Plain text</a>"}}</div>

will result in

<div>{{ $str }}</div>
<div>{{"<a>Plain text</a>"}}</div>

StackOverflow Answer

1
votes

One way of doing this would be to change blade's content tag. Like so :

Blade::setContentTags('[%', '%]');

So that {{}} tags will be considered as plain string. For further information refer this snippet: laravel-recipes