简体   繁体   English

如何使用基于开发/构建上下文的Assemble包含不同的内容部分?

[英]How to include different content partials with Assemble based on dev/build context?

I've jumped into assemble /Grunt to try and improve my workflow for creating templates for the CMS I use. 我跳进了assemble / Grunt尝试改进我为我使用的CMS创建模板的工作流程。 What I'm trying to figure out: is it possible to use a block/partial of HTML content in my template while developing (ie, during "grunt watch"), but then replace that with the include tag that my CMS requires in the final HTML output (ie when i do "grunt build"). 我想弄清楚的是:是否可以在开发时(即在“grunt watch”期间)在我的模板中使用块/部分HTML内容,但是然后用我在CMS中需要的include标记替换它最终的HTML输出(即当我做“grunt build”时)。 Something like the following? 像下面这样的东西?

<div id="content">
{{! if in dev context, include this partial }}
{{#if}}
  {{> body }}
{{! if in build context, include this partial }}
{{else}}
  {{> body-cms-tag }}
{{/if}}
</div>

which if in dev/watch mode, would output 如果在dev / watch模式下,将输出

<div id="content">
  <h1>Test Headline</h1>
  <p>This is just test content.</p>
</div<

but in build mode, would output 但在构建模式下,会输出

<div id="content">
  <?php echo $mContext['bodyElements']; ?>
</div>

Is that possible, either with Handlebars syntax, an Assemble helper, or a Grunt task (similar to grunt-usemin?) 这是可能的,使用Handlebars语法,汇编助手或Grunt任务(类似于grunt-usemin?)

You can add a flag in your data or assemble options and check the value of that flag in your if statement: 您可以在数据或汇编选项中添加标志,并在if语句中检查该标志的值:

Gruntfile.js Gruntfile.js

assemble: {
  options: {
    production: false
  },
  files: { ... }
}

page.hbs page.hbs

<div id="content">
{{! if in dev context, include this partial }}
{{#if production}}
  {{> body }}
{{! if in build context, include this partial }}
{{else}}
  {{> body-cms-tag }}
{{/if}}
</div>

Currently, if you want to get to that production flag inside some helper or partial that changes the level of the context, you'll have to use something like ../production which can be a pain. 目前,如果你想在一些助手或部分内部获得production标志,这会更改上下文的级别,你将不得不使用类似../production东西,这可能很痛苦。 However, Handlebars.js has a feature that will be in a version (hopefully soon) that'll let you do @root.production from any level. 但是,Handlebars.js有一个版本(希望很快)的功能,它可以让你从任何级别执行@root.production This has been merged in but it's not in a release yet. 这已合并,但尚未发布。 When it's release, we'll be updating to that version. 当它发布时,我们将更新到该版本。

Hope this helps. 希望这可以帮助。

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

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