Meteor 如何使用可变模板创建可重用的UI?
我想这样做: 组件文件:Meteor 如何使用可变模板创建可重用的UI?,meteor,spacebars,Meteor,Spacebars,我想这样做: 组件文件: <template name="drop_down"> <span class="dropdown"> {{> primary_element}} <--- load the template named button <span class="dropdown-panel"> {{> panel_template}} <--- load the
<template name="drop_down">
<span class="dropdown">
{{> primary_element}} <--- load the template named button
<span class="dropdown-panel">
{{> panel_template}} <--- load the template named button_panel
</span>
</span>
</template>
{{> drop_down primary_element="button" panel_template="button_panel"}}
<template name="button"> some styled button </template>
<template name="button_panel"> panel content </template>
{{>primary\u element}}panel\u template}}下拉菜单primary\u element=“button”panel\u template=“button\u panel”}
一些样式的按钮
面板内容
然后我可以像这样重复使用它
{{> drop_down primary_element="tmp_btn_2" panel_template="tmp_panel_2"}}
<template name="tmp_btn_2"> another button style </template>
<template name="tmp_panel_2"> other panel content </template>
{>drop\u down primary\u element=“tmp\u btn\u 2”panel\u template=“tmp\u panel\u 2”}
另一种按钮样式
其他小组内容
您应该可以使用。在blaze中,您可以传入要用作变量的模板以及数据上下文本身。它们相当灵活
例如:
{{> Template.dynamic template=whichTemplate data=myData }}
这将引用whichTemplate
帮助程序,以确定要使用哪个模板,并从myData
帮助程序获取数据上下文。i、 e.模板的选择和数据的选择都来自变量
在本例中,您尝试在下拉列表中使用两个动态模板。你可以做:
<template name="drop_down">
{{#with myElements}}
<span class="dropdown">
{{> Template.dynamic template=this.primary}}
<span class="dropdown-panel">
{{> Template.dynamic template=this.panel}}
</span>
</span>
{{/with}}
</template
它将作为数据变量传递到模板。你能在你的答案中举个例子吗?谢谢,这就是我要找的,但是在myElement中它应该返回| | primary:this.primary|元素,panel:this.panel|u template | |,所以我可以动态使用它。是的,这就是在函数中使用它的意义所在,你可以返回你需要的任何东西。
Template.dropdown.helpers({
myElements() {
let p1 = "button"; // compute these two based on your own logic
let p2 = "button_panel";
return {primary: p1, panel: p2 };
}
});