From f71260207dc0c2894d81ff4beca522a0c6ad821b Mon Sep 17 00:00:00 2001 From: ernscht Date: Tue, 22 Mar 2016 08:29:17 +0100 Subject: [PATCH] #50 add placeholder handlebars helper --- app/templates/app/core/config.js | 3 +- app/templates/app/helpers/placeholder.js | 44 ++++++++++++++++++++++ app/templates/app/helpers/view_list.js | 1 + app/templates/project/docs/nitro.md | 10 ++++- app/templates/views/_placeholders/.gitkeep | 0 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 app/templates/app/helpers/placeholder.js create mode 100644 app/templates/views/_placeholders/.gitkeep diff --git a/app/templates/app/core/config.js b/app/templates/app/core/config.js index 9d3dfb600..08c76774b 100644 --- a/app/templates/app/core/config.js +++ b/app/templates/app/core/config.js @@ -15,7 +15,8 @@ function factory() { view_directory: 'views', view_file_extension: '<%= options.viewExt %>', view_partials_directory: 'views/_partials', - view_data_directory: 'views/_data' + view_data_directory: 'views/_data', + placeholders_directory: 'views/_placeholders' }, config.nitro); config.server = { diff --git a/app/templates/app/helpers/placeholder.js b/app/templates/app/helpers/placeholder.js new file mode 100644 index 000000000..0b61d83f9 --- /dev/null +++ b/app/templates/app/helpers/placeholder.js @@ -0,0 +1,44 @@ +var fs = require('fs'); +var hbs = require('hbs'); +var path = require('path'); +var cfg = require('../../app/core/config.js'); +var utils = require('../core/utils'); + +module.exports = function () { + + try { + var context = arguments[arguments.length - 1]; + var name = 'string' === typeof arguments[0] ? arguments[0] : context.hash.name; + var templateFile = 'string' === typeof arguments[1] ? arguments[1] : context.hash.template; + + if(!name) { + throw new Error('Placeholder name parameter not set'); + } + + if(!templateFile) { + throw new Error('Placeholder template parameter not set'); + } + + templateFile += '.' + cfg.nitro.view_file_extension; + + var templatePath = path.join( + cfg.nitro.base_path, + cfg.nitro.placeholders_directory, + name, + templateFile); + + if (utils.fileExistsSync(templatePath)) { + return new hbs.handlebars.SafeString( + hbs.handlebars.compile( + fs.readFileSync(templatePath, 'utf8') + )(context) + ); + } + + throw new Error('Placeholder ' + templatePath + ' not found.'); + + } + catch (e) { + return utils.logAndRenderError(e); + } +}; diff --git a/app/templates/app/helpers/view_list.js b/app/templates/app/helpers/view_list.js index 5a1ed3f02..27e9978c5 100644 --- a/app/templates/app/helpers/view_list.js +++ b/app/templates/app/helpers/view_list.js @@ -11,6 +11,7 @@ var excludes = { directories: [ path.basename(cfg.nitro.view_data_directory), path.basename(cfg.nitro.view_partials_directory), + path.basename(cfg.nitro.placeholders_directory), '.svn' ], files: [ diff --git a/app/templates/project/docs/nitro.md b/app/templates/project/docs/nitro.md index 0e449bb31..326f5ee74 100644 --- a/app/templates/project/docs/nitro.md +++ b/app/templates/project/docs/nitro.md @@ -123,6 +123,14 @@ Render a partial (HTML snippet). Partials are placed in `views/_partials/` as `* {{> head}} +### Render Placeholders + +Using a placeholder is another way to output some markup. Placeholders are placed in a folder inside `views/_placeholders/` as `*.<%= options.viewExt %>` files. +The following two examples do the same and render the file `Content/example.<%= options.viewExt %>` from `views/_placeholders/`. + + {{placeholder 'Content' 'example'}} + {{placeholder name='Content' template='example'}} + ### Passing data #### Data per view @@ -376,7 +384,7 @@ Nitro uses [Gulp](http://gulpjs.com/) under the hood and can therefore be used o The following packages are always installed by the [app](#name) generator: -* [jQuery 2.2.1](http://jquery.com/) +* [jQuery 2.2.2](http://jquery.com/) * [TerrificJS 3.0.0-beta.9](https://github.com/brunschgi/terrificjs)<% if (options.clientTpl) { %> * [Handlebars 4.0.5](https://github.com/components/handlebars.js)<% } %> diff --git a/app/templates/views/_placeholders/.gitkeep b/app/templates/views/_placeholders/.gitkeep new file mode 100644 index 000000000..e69de29bb