Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was looking at your code in the gist and some of the options don't match with the filenames you're referencing.</p> <p>Here is my stab at updating the code you provided to allow rendering partials:</p> <h3>grunt.js</h3> <p>The src is the list of pages you're building that might contain partials In this case, components.mustache would be located at 'docs/components/templates/pages/components.mustache'</p> <p>Updating the layout option to layout.mustache which is used for all the pages (including components.mustache)</p> <p>Adding a paths object to options which has a path to the partials folder. All these partials will be read and compiled and stored in options.partials for later use in the grunt task.</p> <pre><code>module.exports = function(grunt) { 'use strict'; // Project configuration grunt.initConfig({ pkg: '&lt;json:package.json&gt;', meta: { banner: '/**\n' + '* &lt;%= pkg.name %&gt;.js v&lt;%= pkg.version %&gt; by @fat &amp; @mdo\n' + '* Copyright &lt;%= grunt.template.today("yyyy") %&gt; &lt;%= pkg.author %&gt;\n' + '* http://www.apache.org/licenses/LICENSE-2.0.txt\n' + '*/' }, // Build HTML docs from .mustache files hogan: { production: { src: 'docs/components/templates/pages/*.mustache', dest: 'docs/components/FILE.html', options: { title: 'Sellside', url: 'docs', setAccount: 'NA', setSiteId: 'NA', layout: 'docs/components/templates/layout.mustache', dev: true, docs: true, app: false, website: false, paths: { partials: 'docs/components/templates/partials/*.mustache' } } } } }); // Load npm tasks. grunt.loadNpmTasks('grunt-contrib'); // Load local tasks. grunt.loadTasks('tasks'); grunt.registerTask('default', 'hogan'); }; </code></pre> <h3>hogan.js</h3> <p>Updating this task to read in all the partials and compile them.</p> <p>The helper is being updated to add the 'body' partial (which is the compiled page) to the options.partials list.</p> <p>The options.partials is then passed into the hogan.render method so all the partials are available to all the pages.</p> <pre class="lang-js prettyprint-override"><code>/* * Build HTML from mustache files * https://github.com/sellside/ui/grunt.js * * Copyright (c) 2012 Sellside * Authored by Jon Schlinkert */ module.exports = function(grunt) { // Grunt utilities. var task = grunt.task, file = grunt.file, utils = grunt.util, log = grunt.log, verbose = grunt.verbose, fail = grunt.fail, option = grunt.option, config = grunt.config, template = grunt.template, _ = utils._ // external dependencies var fs = require('fs'), hogan = require('hogan'); // ========================================================================== // TASKS // ========================================================================== grunt.registerMultiTask('hogan', 'Compile mustache files to HTML with hogan.js', function() { var data = this.data, src = grunt.file.expandFiles(this.file.src), dest = grunt.template.process(data.dest), // Options are set in gruntfile defaults = { production: false, docs: false, title: 'Sellside', setAccount: 'NA', setSiteId: 'NA', layout: 'docs/templates/layout.mustache', paths: {}, partials: {} }, options = _.extend(defaults, this.data.options || {}) !src &amp;&amp; grunt.warn('Missing src property.') if(!src) return false !dest &amp;&amp; grunt.warn('Missing dest property') if(!dest) return false var done = this.async() var srcFiles = file.expandFiles(src) if(options.paths.partials) { var partials = grunt.file.expandFiles(options.paths.partials); log.writeln('Compiling Partials...'); partials.forEach(function(filepath) { var filename = _.first(filepath.match(/[^\\\/:*?"&lt;&gt;|\r\n]+$/i)).replace(/\.mustache$/, ''); log.writeln(filename.magenta); var partial = fs.readFileSync(filepath, 'utf8'); options.partials[filename] = hogan.compile(partial); }); log.writeln(); } try { options.layout = fs.readFileSync(options.layout, 'utf8') options.layout = hogan.compile(options.layout, { sectionTags: [{ o: '_i', c: 'i' }] }) } catch(err) { grunt.warn(err) &amp;&amp; done(false) return } srcFiles.forEach(function(filepath) { var filename = _.first(filepath.match(/[^\\\/:*?"&lt;&gt;|\r\n]+$/i)).replace(/\.mustache$/, '') grunt.helper('hogan', filepath, filename, options, function(err, result) { err &amp;&amp; grunt.warn(err) &amp;&amp; done(false) if(err) return file.write(dest.replace('FILE', filename), result) }) }) done() }) // ========================================================================== // HELPERS // ========================================================================== grunt.registerHelper('hogan', function(src, filename, options, callback) { log.writeln('Compiling ' + filename.magenta); var page = fs.readFileSync(src, 'utf8'), html = null, layout = options.layout, context = {}; context[filename] = 'active'; context._i = true; context.production = options.production; context.docs = options.docs; context.setAccount = options.setAccount; context.setSiteId = options.setSiteId; var title = _.template("&lt;%= page == 'Index' ? site : page + ' · ' + site %&gt;") context.title = title({ page: _(filename).humanize().replace('css', 'CSS'), site: options.title }) try { page = hogan.compile(page, { sectionTags: [{ o: '_i', c: 'i' }] }) options.partials.body = page; page = layout.render(context, options.partials) callback(null, page) } catch(err) { callback(err) return } }) }; </code></pre> <p>One thing to note, if you're going to pass data into the partials, you'll need to add that to the context object in the file layout.render call.</p> <p>Hope this all make sense and helps you out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload