Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates Meteor:如果存在上下文,则在模板内设置上下文_Templates_Meteor_Routes - Fatal编程技术网

Templates Meteor:如果存在上下文,则在模板内设置上下文

Templates Meteor:如果存在上下文,则在模板内设置上下文,templates,meteor,routes,Templates,Meteor,Routes,我的问题是,我希望为/edit/:id路由和/add路由呈现相同的表单模板。后者应该为表单提供空输入,第一个应该使用对象的现有属性填充输入字段,比如说它是一个Post 保持干燥,我不想复制粘贴到一个新的模板模板 为此,我采取了以下措施: <template name="addOrEditPost"> {{#if this.post}} {{#with this.post}} {{/if}} <form> <input type="text

我的问题是,我希望为/edit/:id路由和/add路由呈现相同的表单模板。后者应该为表单提供空输入,第一个应该使用对象的现有属性填充输入字段,比如说它是一个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>

它正在工作。但是,由于表单有大约50个输入字段,因此上下文将更加清晰…

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

<template name="addOrEditPost">
  {{#if this.post}}
    {{> form this.post}}
  {{else}}
    {{> form}}
  {{/if}}
</template>
如果有一个Post,我可以在表单模板中使用{{whatever}}调用它的属性,因为this.Post作为参数传递给模板,并作为其上下文

如果你对如何改进这一点有想法,请分享你的想法。谢谢

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