同时显示和不显示Mustache模板

同时显示和不显示Mustache模板

本文介绍了使用Laravel/Blade和JavaScript,同时显示和不显示Mustache模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel,而我所有的模板都在使用Blade模板.页面的一部分需要在服务器端和JavaScript上呈现.对于这些,我使用的是Mustache模板(带有 mustache-l4 ).

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).

问题在于如何让Laravel渲染模板,又如何将它们包含在页面中并带有完整的Mustache标签,以供JS提取.

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.

例如,如果我有一个这样的(简化示例)Blade模板:

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>

其中包括这样的.mustache部分:

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

Mustache部分在页面加载良好时呈现.但是,当它包含在<script></script>标记之间时,我希望将其逐字呈现.即,{{#some_text}} Mustache标签应该在呈现的HTML中.然后,JavaScript可以将其作为Moustache模板读取.

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.

我可以通过在服务器端(Blade)模板上更改标签定界符,但是在现阶段,随着数十个Blade模板已经在工作,我宁愿不这样做.

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.

我不知道该怎么做.我可以只更改其中一部分的"Mustache"定界符,然后才将其完全渲染吗?

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?

推荐答案

另一种方法是使用@

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

将导致

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

StackOverflow答案

这篇关于使用Laravel/Blade和JavaScript,同时显示和不显示Mustache模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:08