7

Meteor には 3 つの単純なテンプレートがあり、サーバーには任意の名前の組み合わせを持つコレクションがあります。コレクションに含まれる名前に基づいて、これらのテンプレートを動的にレンダリングできるようにしたいと考えています。

現在、クライアントを使用してコレクションをサブスクライブし、テンプレート関数を介して名前にアクセスすることで、これを達成しようとしています。残念ながら、名前に対して">"を実行しようとすると、Meteor はその値が指すテンプレートではなく、変数名をレンダリングしようとします。

したがって、テンプレート1 、テンプレート2 、およびテンプレート3 でhtml をレンダリングする代わりに、出力は単にページ上の名前「テンプレート 1 テンプレート 2 テンプレート 3」になります。

これが私が使用しているコードです。Meteor.render を手動で実行しなくても問題を解決できる方法があることを願っています。

サーバーjs:

TemplatesToRender = new Meteor.Collection("templatesToRender");

TemplatesToRender.insert({templateName: "template3"});
TemplatesToRender.insert({templateName: "template2"});

クライアント html:

<body>
    {{#each templatesToRender}}
        {{> templateName}}           // meteor trying to render a template
                                     // called "templateName" instead of the 
                                     // variable inside templateName.
    {{/each}}
</body>

<template name="template1">
    <span>Template 1</span>
</template>

<template name="template2">
    <span>Template 2</span>
</template>

<template name="template3">
    <span>Template 3</span>
</template>
4

3 に答える 3

4

renderあなたはヘルパーを作ることができます:

 Handlebars.registerHelper('render', function(name, options) {
   if (Template[name])
     return new Handlebars.SafeString(Template[name]());
 });

そしてそれを

{{render templateName}}
于 2012-11-29T06:27:48.333 に答える
0

あなたはこれを試してみたいかもしれません

あなたのhtmlで

<body>

    {{> templateToRender}}

</body>

<template name="templateToRender">

    {{! use below to detect which template to render}}

    {{#if templateName "template1"}}
        {{> template1}}
    {{/if}}

    {{#if templateName "template2"}}
        {{> template3}}
    {{/if}}

    {{#if templateName "template3"}}
        {{> template3}}
    {{/if}}

</template

<template name="template1">

    <p>this is template1</p>

</template>

<template name="template2">

    <p>this is template2</p>

</template>

<template name="template3">

    <p>this is template3</p>

</template>

あなたのスクリプトで

Template.templateToRender.templateName = (which) ->
    # if user have a field like templateName you can do things like
    tmplName = Meteor.user().templateName
    # Session.equals will cause a template render if condition is true.
    Session.equals which, tmplName
于 2012-11-24T03:44:37.310 に答える