如何根据开发/构建上下文包含不同的内容部分



我已经跳入组装/咕unt,试图改善我使用的CMS模板的工作流程。我要弄清楚的是:开发时是否可以在模板中使用html内容的块/部分(在" grunt手表"中),然后用我的CMS需要的包含标签替换为最终的HTML输出(即进行" Grunt Build"时)。类似以下内容?

<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>

如果在开发/观察模式下,将输出

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

但是在构建模式下,将输出

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

有可能使用车把语法,组装助手或grunt任务(类似于grunt-usemin?)

您可以在数据中添加标志或组装选项,并在if语句中检查该标志的值:

gruntfile.js

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

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>

当前,如果您想在某个改变上下文级别的帮手或部分辅助人员或部分中到达该production标志,则必须使用诸如../production之类的东西,这可能会很痛苦。但是,handlebars.js的功能将在版本中(希望很快),可以让您从任何级别进行@root.production。这已经合并了,但还没有发行。发布时,我们将更新到该版本。

希望这会有所帮助。

最新更新