【问题标题】:Passing content to nested partials in assemble在组装中将内容传递给嵌套的部分
【发布时间】:2014-05-08 20:57:01
【问题描述】:

我正在使用 assemble 为新网站制作原型。

我想彻底模块化我的代码,就像 Brad Frost 用他的 pattern lab 传福音一样。

示例

基本上我想要一个在英雄局部(“分子”)中使用的标题部分(模式实验室中的“原子”):

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 文件中传递数据。对于我的页面,我使用提供块的默认布局。例如:

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}} 。这有可能吗?还是我的方法(我在这个非常简单的例子中描述的)完全错了?

为任何指针干杯! :-)

【问题讨论】:

  • 你有我可以看到的公共存储库吗?我认为这是直截了当的,但我想在给出更长的答案之前对其进行测试。
  • 嗨@doowb,感谢您回复我。恐怕我没有公开回购,因为它是客户工作。我的要求只是将不同的文本 sn-ps 传递给来自 homepage.hbs、productpage.hbs 等的英雄部分内的标题部分。

标签: json handlebars.js assemble


【解决方案1】:

@polarbirke 因为您想使用来自myPageWithHero.json 的数据,所以在渲染myPageWithHero.hbs 时,该数据将在page 对象上可用,因此您可以将该对象传递给hero 部分。这将为该部分设置上下文,title 部分将继承该上下文:

{{#extend "base"}}
  {{#content "hero"}}
    {{> hero page}}
  {{/content}}
  {{#content "body"}}
    {{!-- myPageContent --}}
  {{/content}}
{{/extend}}

如果您想要使用数据中的其他对象,则可以改为传递它:

数据.json

{
  "title1": {
    "class": "success",
    "text": "Good Title"
  },
  "title2": {
    "class": "error",
    "text": "Bad Title"
  }
}

myPageWithHero.hbs

{{#extend "base"}}
  {{#content "hero"}}
    {{> hero title1}}
  {{/content}}
  {{#content "body"}}
    {{!-- myPageContent --}}
  {{/content}}
{{/extend}}

希望对您有所帮助,如果您有任何问题,请告诉我。

【讨论】:

  • 谢谢,确实有帮助!很抱歉没有早点回来,我不得不花时间在另一个项目/截止日期上,然后才能重新开始这个项目。我现在将愉快地回到组装世界。 :)
猜你喜欢
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多