简体   繁体   English

将参数传递给 Blaze 模板

[英]Passing arguments to a Blaze template

Say I have a common piece of markup that will be repeated throughout my website with only a few small differences.假设我有一个通用的标记,它将在我的网站中重复出现,只有一些小的差异。

For example, I have a bunch of <div> elements that all have a title.例如,我有一堆<div>元素,它们都有一个标题。

Elements with class: section-title are all styled the same way, but their text content is different.带有 class: section-title元素的样式都相同,但它们的文本内容不同。

I would like to make a template to represent a title, sort of like this:我想制作一个模板来表示标题,就像这样:

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

Except, I need "Profile" to be interchangeable, because I have many different sections: "profile", "contact information", etc.除了,我需要“个人资料”可以互换,因为我有很多不同的部分:“个人资料”、“联系信息”等。

What is the best way to do this in Blaze?在 Blaze 中执行此操作的最佳方法是什么?

You can use either template arguments or set the template current data context to the appropriate list of arguments.您可以使用模板参数或将模板当前数据上下文设置为适当的参数列表。

HTML HTML

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

{{> sectionTitle title="Profile"}}

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

JS JS

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM