Skip to content

Commit

Permalink
#50 add placeholder handlebars helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ernscht committed Mar 22, 2016
1 parent a0bef2c commit f712602
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/templates/app/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
44 changes: 44 additions & 0 deletions app/templates/app/helpers/placeholder.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
1 change: 1 addition & 0 deletions app/templates/app/helpers/view_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
10 changes: 9 additions & 1 deletion app/templates/project/docs/nitro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)<% } %>

Expand Down
Empty file.

0 comments on commit f712602

Please sign in to comment.