【问题标题】:Using template inheritance with Assemble在 Assemble 中使用模板继承
【发布时间】:2013-08-02 11:15:26
【问题描述】:

是否可以在模板中定义区域,以便从页面中提取内容?

假设我的模板中有以下结构:

<div class=sidebar></div>
<div class=content></div>

从页面内容中,我想拉一些html内容到侧边栏,其他内容拉到内容div。

这可能吗?

【问题讨论】:

  • 这个模板从何而来?你打算用什么语言从页面中提取内容? Javascript?
  • @NoLifeKing 我的项目具有以下结构:templates/layouts/default.hbs 和页面:templates/pages/*.hbs 一般的想法是在我的内容上强制执行 html 结构,但在模板级别定义该结构,所以我不需要在每个页面中重复该结构。
  • @LuisMartins,我回答你的问题了吗?

标签: handlebars.js template-engine gruntjs assemble


【解决方案1】:

以 Swig 作为引擎

是的,这是可能的。请参阅 https://github.com/assemble/boilerplate-swig,特别是 this example,它展示了如何使用 {% macro %} 标签来完成您所询问的内容。

如果您想使用 Swig,请务必将自述文件也视为 assemble-swig 存储库。您必须将 swig 注册为 assemble 中的当前引擎:

assemble: {
  options: {
    engine: 'swig'
  }
}

以把手作为引擎

如果侧边栏内容始终相同,则在每个页面上,您都可以使用部分内容。即使每个页面上的 URL 或活动类都发生了变化,这也应该有效。

但是,使用模板或“块”继承的动态内容,例如extend 可以通过Handlebars helpers 实现。

但是由于layoutsassemble 一起使用,因此使用“开箱即用”的助手有点棘手。澄清一下,我能想到的几乎所有助手都可以很好地使用 assemble 开箱即用,除了这个 - 特别是因为布局的工作方式。

我的建议是您在 assemble 和/或 handlebars-helpers 项目中为此添加现有功能请求,以添加您的用例和对您想要实现的目标的想法:

【讨论】:

    【解决方案2】:

    @jonschlinkert 你应该更新 assemble 的文档,因为从 Assemble 开始并不那么容易,而且很多东西都没有隐藏。

    所以 Luis,你可以试试这个方法,目前对我也很有效!

    Assemble: Multiple points of content insertion in layout?

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 2018-05-23
      • 2015-11-13
      • 2010-09-08
      • 2014-06-19
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多