【问题标题】:Passing arguments to a Blaze template将参数传递给 Blaze 模板
【发布时间】:2015-09-22 07:17:27
【问题描述】:

假设我有一个共同的标记,它将在我的网站中重复,只有一些小的差异。

例如,我有一堆 <div> 元素,它们都有一个标题。

class: section-title 的元素的样式都相同,但它们的文本内容不同。

我想做一个模板来代表一个标题,有点像这样:

<template name="section-title">
  <div class="section-title">
    <span> Profile </span>
  </div>
</div>

除了,我需要“个人资料”可以互换,因为我有很多不同的部分:“个人资料”、“联系信息”等。

在 Blaze 中执行此操作的最佳方法是什么?

【问题讨论】:

标签: javascript html templates meteor meteor-blaze


【解决方案1】:

您可以使用模板参数或将模板当前数据上下文设置为适当的参数列表。

HTML

<template name="sectionTitle">
  <section class="section-title">
    <span>{{title}}</span>
  </section>
</template>

{{> sectionTitle title="Profile"}}

<template name="parent">
  {{> sectionTitle args}}
</template>

JS

Template.parent.helpers({
  args: function(){
    return {
      title: "Profile"
    };
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2010-12-26
    相关资源
    最近更新 更多