【问题标题】:How to include different content partials with Assemble based on dev/build context?如何根据开发/构建上下文在 Assemble 中包含不同的内容部分?
【发布时间】:2014-02-26 14:00:27
【问题描述】:

我已加入assemble/Grunt 尝试改进我的工作流程,以便为我使用的 CMS 创建模板。我想弄清楚的是:是否可以在开发时(即,在“grunt watch”期间)在我的模板中使用 HTML 内容的块/部分,然后用我的 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>

这可能吗,无论是使用 Handlebars 语法、Assemble 助手还是 Grunt 任务(类似于 grunt-usemin?)

【问题讨论】:

    标签: gruntjs handlebars.js assemble grunt-assemble


    【解决方案1】:

    您可以在您的数据或组合选项中添加一个标志,并在您的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。这已被合并,但尚未发布。当它发布时,我们将更新到该版本。

    希望这会有所帮助。

    【讨论】:

    • 在 v0.5.0 中可以使用助手进行布局/正文部分吗?我会看一些,如果是这样,那是另一种选择......一个条件帮助器,在开发模式下呈现参数 A,在生产模式下呈现参数 B。
    • 感谢大家的帮助 - 看起来我快到了,只是需要 grunt 文件中缺少的标志 - 有道理......干杯!
    • 这仍然需要我记住在发布后将 options.production 标志设置为 true,对吧?有没有办法在运行grunt 时自动触发生产标志,但在运行grunt watch 时标志关闭?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2011-09-27
    • 1970-01-01
    • 2016-05-29
    • 2014-01-19
    • 1970-01-01
    • 2015-08-04
    相关资源
    最近更新 更多