简体   繁体   English

我可以从Mustache.js中的另一个文件加载部分内容吗?

[英]Can I load partials from another file in Mustache.js?

I'm trying to load in partials from a separate file while using mustache.js but it's proven difficult. 我正在尝试使用mustache.js从单独的文件加载部分文件,但事实证明这很困难。 The PHP implementation makes this very easy, but not so much in the JS side. PHP实现使这很容易,但在JS方面却没有那么多。

I've gone through the docs and can't find anything related to this, only using an object as a partial instead of a file. 我已经浏览了文档,找不到与此相关的任何内容,只使用对象作为部分而不是文件。

Here's some sort of psuedocode to explain what I'm trying to do. 这是一种用于解释我正在尝试做什么的伪代码。

$.Mustache.load('/mustaches.php', function () {

    var item = { thing: 'one' };
    var partial = 'partials/actions.mustache';

    $('.container').mustache(
        'main_mustache',
        {item: item},
        partial
    );

});

So, I'd like to be able to load a secondary file as a partial an include it with the template to be used so I don't have to duplicate code across all the different templates I use. 因此,我希望能够将辅助文件作为部分加载,并将其包含在要使用的模板中,这样我就不必在我使用的所有不同模板中复制代码。

With something like Blaze I can import a template with {{> templateName}} but it doesn't seem to be quite so easy with Mustache.js. 使用像Blaze这样的东西,我可以使用{{> templateName}}导入模板,但使用Mustache.js似乎并不那么容易。

If this isn't possible with Mustache, what other libraries would you recommend? 如果使用Mustache无法做到这一点,您会推荐其他哪些图书馆?

You could load both templates with mustache, and then pass one of them (the partial) to the one that should render the partial. 您可以使用小胡子加载两个模板,然后将其中一个(部分)传递给应该呈现部分的模板。 Be aware that in order to make it work, in the mustache where you want to render the partial, the variable name should be enclosed in triple curly bracers in order to tell mustache that it should be html: 请注意,为了使其工作,在要呈现部分的小胡子中,变量名称应该包含在三重花括号中,以告诉小胡子它应该是html:

<div>
    <h1>This is my title</h1>
    {{{partial}}}
</div>

and then: 然后:

$('.container').mustache(
    'main_mustache',
    {item: item, partial: partial},
);

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

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