gpt4 book ai didi

javascript - meteor .js : how to pass the data context of one helper to another helper?

转载 作者:行者123 更新时间:2023-11-29 19:46:43 24 4
gpt4 key购买 nike

所以如果我有一个模板:

<template name="myTemplate">
{{foo}}
</template>

和模板助手:

Template.myTemplate.foo = function() {
blah = Session.get('blah');
// code to do stuff with blah
return blah;
};

然后我有另一个模板:

<template name="myOtherTemplate">
{{foo}}
</template>

我希望此模板的数据上下文与之前的模板相同,我该怎么做?

我首先想到使用 {{#with}} 可能是正确的方法,但这似乎只有在第二个模板的范围已经在第一个模板中时才有效。

最终,我希望能够在另一个模板中使用为一个模板定义的所有助手,并且知道如何做到这一点。

最佳答案

您似乎在问以下两个问题之一:

  1. 如果您在 myTemplate 中使用 myOtherTemplate,则其他模板的上下文将与此模板相同,除非您显式将其他内容传递给它部分的第二个参数。

    <template name="myTemplate">
    {{> myOtherTemplate foo}}
    </template>
  2. 如果您想在多个模板中使用一个助手,请在全局助手中声明它。这将使 {{foo}} 在所有模板中可用:

    Handlebars.registerHelper("foo", function() {
    blah = Session.get('blah');
    // code to do stuff with blah
    return blah;
    });
  3. 如果您想动态创建自己的数据上下文(这种情况很少见),请执行以下操作:

    <template name="myTemplate">
    {{{customRender}}}
    </template>

    Template.myTemplate.customRender = function() {
    return Template.otherTemplate({
    foo: something,
    bar: somethingElse,
    foobar: Template.myTemplate.foo // Pass in the helper with a different name
    });
    };

    这个对象基本上就是 Iron-Router 将在渲染时传递给您的模板的对象。请注意,您将需要使用三 Handlebars {{{ }}} 或使用 new Handlebars.SafeString 来告诉它不要转义模板。

    <

关于javascript - meteor .js : how to pass the data context of one helper to another helper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18987763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com