流星:如果上下文存在,则在模板内部设置上下文

问题描述:

我遇到了问题,我想为/edit/:id -route和/add -route呈现相同的表单模板。后者应该为表单提供空的输入,第一个应该填充输入字段和对象的现有属性,假设它是Post。流星:如果上下文存在,则在模板内部设置上下文

保持干燥我不想复制粘贴模板到一个新的。

要做到这一点,我做了以下内容:

<template name="addOrEditPost"> 
    {{#if this.post}} 
    {{#with this.post}} 
    {{/if}} 
    <form> 
    <input type="text" value={{#if name}}{{name}}{{/if}} ...> 
    ... 
    </form> 
    {{#if this.post}} 
    {{/with}} 
    {{/if}} 
</template> 

我的想法是,只提供一个模板背景下,如果上下文本身存在(如果存在的话,我们在“编辑”如果。不,我们在'添加')。

但这通向

=> Errors prevented startup: 

While building the application: 
client/views/admin/addPost.html:6: Expected tag to close with, found if 
...{#with this.post}} {{/if}} <form id="... 
^ 

所以,我怎么能做到这一点,而无需复制整个模板和形成的?提前致谢!

编辑:

我不得不提到它的工作不{{with}} -context:当我写

<input type="text" value={{#if this.post.name}}{{this.post.name}}{{/if}} ...> 

这是工作。但尽管如此,上下文会更清洁,因为形式有大约50输入字段...

好吧,我想出了我自己的解决方案:

<template name="addOrEditPost"> 
    {{#if this.post}} 
    {{> form this.post}} 
    {{else}} 
    {{> form}} 
    {{/if}} 
</template> 

如果有一个帖子,我可以在form模板内使用{{whatever}}调用其属性,因为this.post作为参数传递给模板并用作其上下文。

如果您有什么想法仍然可以改善这一点,请分享您的想法。谢谢。