5

I've jumped into assemble/Grunt to try and improve my workflow for creating templates for the CMS I use. 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"). 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

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

jonschlinkert
  • 10,872
  • 4
  • 43
  • 50
michaelroper
  • 293
  • 1
  • 6

1 Answers1

3

You can add a flag in your data or assemble options and check the value of that flag in your if statement:

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>

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. However, Handlebars.js has a feature that will be in a version (hopefully soon) that'll let you do @root.production from any level. 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.

doowb
  • 3,372
  • 1
  • 17
  • 25
  • in v0.5.0 could a helper be used for the layout/body partial? I'll take a look in a few, if so that's another option... a conditional helper that renders param A when in dev mode, or param B when in prod. – jonschlinkert Feb 04 '14 at 02:04
  • Thanks for the help guys - looks like I was almost there, just needed that missing flag in the grunt file - makes sense... cheers! – michaelroper Feb 05 '14 at 04:18
  • 1
    that still needs me to remember to set the options.production flag to true once I release, right? Is there a way to automatically trigger the production flag when running `grunt` but when running `grunt watch` the flag is off? – hansaplast Feb 05 '14 at 16:29