问题描述
我正在使用组装制作新站点的原型.
I'm using assemble for prototyping a new site.
我想彻底地模块化我的代码,就像布拉德·弗罗斯特(Brad Frost)通过他的模式实验室进行宣传一样.
示例
I would like to modularize my code quite drastically, much like Brad Frost is evangelizing with his pattern lab.
EXAMPLE
基本上,我希望有一个标题部分(在模式实验室中称为原子"),该标题用于英雄部分(分子")中:
Basically I would like to have a title partial ("atom" in pattern-lab speak) which is used inside a hero partial ("molecule"):
title.hbs
<h1 class="{{class}}">{{text}}</h1>
hero.hbs
<section class="hero-unit">
{{!-- image and stuff --}}
<header class="hero-description">
{{> title }}
{{> subTitle }}
</header>
</section>
英雄部分应该是通用的;我想通过每个特定页面的JSON文件传递数据.对于我的页面,我使用提供块的默认布局.例如:
The hero partial is supposed to generic; I want to pass in data from JSON files per particular page. For my pages, I use a default layout that offers blocks. For example:
default.hbs
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
{{#block "hero"}}{{/block}}
{{#block "body"}}{{/block}}
</body>
</html>
myPageWithHero.hbs
{{#extend "default"}}
{{#content "hero"}}
{{ >hero }}
{{/content}}
{{#content "body"}}
{{!-- myPageContent --}}
{{/content}}
{{/extend}}
现在,我想通过我拥有的myPageWithHero.json文件将{{text}}设置在标题部分内,该部分位于英雄部分内.那有可能吗?还是我的方法(在这个非常简单的示例中已描述)完全不合时宜?
Now I'd like to set {{text}} inside the title partial which is inside the hero partial via the myPageWithHero.json file that I have. Is that at all possible? Or is my approach (which I have described in this very oversimplified example) completely deranged?
为任何指针加油! :-)
Cheers for any pointers! :-)
推荐答案
@polarbirke,因为您要使用myPageWithHero.json
中的数据,因此渲染myPageWithHero.hbs
时该数据将在page
对象上可用,因此您可以将该对象传递给hero
部分.这将为该部分设置上下文,而title
部分将继承该上下文:
@polarbirke since you want to use the data from myPageWithHero.json
, that data will be available on the page
object when rendering myPageWithHero.hbs
, so you can pass that object to the hero
partial. This will setup the context for that partial and the title
partial will inherit that context:
{{#extend "base"}}
{{#content "hero"}}
{{> hero page}}
{{/content}}
{{#content "body"}}
{{!-- myPageContent --}}
{{/content}}
{{/extend}}
如果数据中还有其他对象要使用,则可以通过传递该对象:
If you have other objects in your data that you'd like to use instead, you can pass that instead:
data.json
data.json
{
"title1": {
"class": "success",
"text": "Good Title"
},
"title2": {
"class": "error",
"text": "Bad Title"
}
}
myPageWithHero.hbs
myPageWithHero.hbs
{{#extend "base"}}
{{#content "hero"}}
{{> hero title1}}
{{/content}}
{{#content "body"}}
{{!-- myPageContent --}}
{{/content}}
{{/extend}}
希望对您有所帮助.
这篇关于在汇编中将内容传递给嵌套的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!