简体   繁体   English

在mustache.js中重用mustache.php

[英]Reuse mustache.php in mustache.js

I recently started to use Mustache and I'm in a position where I need to reuse PHP templates also in JS. 我最近开始使用Mustache,现在我需要在JS中重用PHP模板。 Although I compiled templates into JS, sometimes is more convenient to just embed templates like this 尽管我将模板编译为JS,但有时像这样嵌入模板会更方便

<script type="text/template" id="my-tpl">
  {{foo}}
</script><!-- /#my-tpl -->

(and then use from JS). (然后从JS使用)。

The problem starts now: since I'm displaying this from Mustache.php, the {{foo}} variable is parsed, therefore is not available from JS anymore. 问题现在开始:由于我是从Mustache.php显示的,因此{{foo}}变量已解析,因此JS不再可用。 The only solution I found is to use different template tags (eg <% foo %> ) for JS, but then this will block the not quite reusable between languages. 我发现的唯一解决方案是对JS使用不同的模板标记(例如<% foo %> ),但这将阻止语言之间不太可重用的代码。

So, my question is: it is possible to display a mustache template inside of a mustache template? 因此,我的问题是:可以在小胡子模板内显示小胡子模板吗? Did I miss something in the docs? 我是否错过了文档中的某些内容? Or it's just not possible? 还是不可能?

Thanks! 谢谢!

You can also switch to different template tags just before the embedded template, and then back after you're done: 您还可以在嵌入模板之前切换到不同的模板标签,然后在完成后切换回:

something awesome with {{ tags }}!

{{=[[[ ]]]=}}
<script type="text/template" id="my-tpl">
  {{foo}}
</script>
[[[={{ }}=]]]

back again with more {{ tags }}!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM