meltmedia / generator-express-static

A yeoman generator for the express-static site builder.
MIT License
2 stars 0 forks source link

Register Handlebars Partials #8

Closed kyleaclark closed 10 years ago

kyleaclark commented 10 years ago

Handlebars views/partials does not appear to be set up by default in the express generation. Here is an example of what I implemented to utilize partials via handlebars:

/* app.js */

// Variable defintions var fs = require('fs'); var hbs = require('handlebars'); var partials = "./views/partials/";

// Register partials fs.readdirSync(partials).forEach(function (file) { var source = fs.readFileSync(partials + file, "utf8"), partial = /(.+).html/.exec(file).pop();

hbs.registerPartial(partial, source); });

/* views / index.html */

{{> partialTest}}

/* views / partials / partialTest.html */

Partial test content

kyleaclark commented 10 years ago

Without the updates shown above to app.js the following error in the browser is returned:

Error: The partial partialTest could not be found at new Error (unknown source) at Error.Exception (/Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/exception.js:6:41) at Object.invokePartial (/Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/runtime.js:135:11) at Object.invokePartialWrapper as invokePartial at Object.eval (eval at (/Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:179:23)) at /Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/runtime.js:92:31 at /Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:467:21 at Function.exports.handlebars.render (/Users/kclark/git_repo/hepcfight/node_modules/consolidate/lib/consolidate.js:495:14) at /Users/kclark/git_repo/hepcfight/node_modules/consolidate/lib/consolidate.js:144:25 at /Users/kclark/git_repo/hepcfight/node_modules/consolidate/lib/consolidate.js:97:5

kyleaclark commented 10 years ago

Without the updates shown above to app.js the following error in the terminal console is returned:

Error: The partial partialTest could not be found at new Error (unknown source) at Error.Exception (/Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/exception.js:6:41) at Object.invokePartial (/Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/runtime.js:135:11) at Object.invokePartialWrapper as invokePartial at Object.eval (eval at (/Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:179:23)) at /Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/runtime.js:92:31 at /Users/kclark/git_repo/hepcfight/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:467:21 at Function.exports.handlebars.render (/Users/kclark/git_repo/hepcfight/node_modules/consolidate/lib/consolidate.js:495:14) at /Users/kclark/git_repo/hepcfight/node_modules/consolidate/lib/consolidate.js:144:25 at /Users/kclark/git_repo/hepcfight/node_modules/consolidate/lib/consolidate.js:97:5 GET / 500 60ms - 1.14kb

kyleaclark commented 10 years ago

I'll make a pull request for the suggested update above.

jacobheun commented 10 years ago

Resolved by #11