+40
-4
| 'use strict'; | ||
| var map = require('map-stream'); | ||
| var es = require('event-stream');; | ||
| var es = require('event-stream'); | ||
| var gutil = require('gulp-util'); | ||
| var Hogan = require('hogan.js'); | ||
| var path = require('path'); | ||
| var fs = require('fs'); | ||
@@ -11,6 +12,41 @@ module.exports = function(data, options, extension) { | ||
| return es.map(function (file, cb) { | ||
| file.contents = new Buffer( Hogan.compile(file.contents.toString(), options).render(data) ); | ||
| var compiled = Hogan.compile(file.contents.toString(), options); | ||
| var partialTemplates = getPartials(compiled, path.dirname(file.path), path.extname(file.path), data, options); | ||
| file.contents = new Buffer( compiled.render(data, partialTemplates) ); | ||
| file.path = gutil.replaceExtension(file.path, extension); | ||
| cb(null,file); | ||
| }); | ||
| }; | ||
| }; | ||
| function getPartials(compiled, dir, ext, data, options) { | ||
| var currentPartials = {}, | ||
| partialTemplates = {}, | ||
| partialPath = '', | ||
| compiledPartial; | ||
| Object.keys(compiled.partials).forEach(function (tag) { | ||
| // find the path of the partial | ||
| partialPath = path.format({ | ||
| 'dir': dir, | ||
| 'base': compiled.partials[tag].name + ext | ||
| }); | ||
| // read and compile the files contents | ||
| compiledPartial = Hogan.compile(fs.readFileSync(partialPath).toString(), options); | ||
| // if partials exist within the compiled tempalte, then | ||
| if (Object.keys(compiledPartial.partials).length !== 0) { | ||
| currentPartials = getPartials(compiledPartial, dir, ext, data, options); | ||
| } | ||
| // Assign the contents of the partial to a key of the same name | ||
| partialTemplates[compiled.partials[tag].name] = compiledPartial.render(data, currentPartials); | ||
| }); | ||
| return partialTemplates; | ||
| } |
+1
-2
| { | ||
| "name": "gulp-hogan", | ||
| "version": "1.2.1", | ||
| "version": "2.0.0", | ||
| "description": "gulp plugin to compile mustache templates", | ||
@@ -28,3 +28,2 @@ "main": "index.js", | ||
| "event-stream": "^3.1.7", | ||
| "map-stream": "^0.0.6", | ||
| "hogan.js": "^3.0.2" | ||
@@ -31,0 +30,0 @@ }, |
+69
-0
@@ -31,2 +31,71 @@ # gulp-hogan [![Build status][travis-image]][travis-url] | ||
| ## Template inheritance & Partials | ||
| Partials will be rendered recursively, allowing for template inheritance. | ||
| ### Example: | ||
| Folder structure | ||
| ``` | ||
| |-- templates | ||
| |-- page.hogan | ||
| |-- body.hogan | ||
| |-- partials | ||
| | |-- header.hogan | ||
| | |-- footer.hogan | ||
| ``` | ||
| page.hogan | ||
| ```html | ||
| <h1>Page</h1> | ||
| {{> body }} | ||
| ``` | ||
| body.hogan | ||
| ```html | ||
| {{> partials/header }} | ||
| {{> partials/footer }} | ||
| ``` | ||
| header.hogan | ||
| ```html | ||
| <header> | ||
| Logo {{#image}}<img>{{/image}} | ||
| </header> | ||
| ``` | ||
| footer.hogan | ||
| ```html | ||
| <footer> | ||
| Copyright {{year}} | ||
| </footer> | ||
| ``` | ||
| gulpfile.js | ||
| ```javascript | ||
| var hogan = require('gulp-hogan'); | ||
| gulp.task('default', function(){ | ||
| gulp.src('template/page.hogan', {}, '.html') | ||
| .pipe(hogan({year: '2016'}, null, '.html')) | ||
| .pipe(gulp.dest('dist')); | ||
| }); | ||
| ``` | ||
| dist/page.html | ||
| ```html | ||
| <h1>Page</h1> | ||
| <header> | ||
| Logo <img> | ||
| </header> | ||
| <footer> | ||
| Copyright 2016 | ||
| </footer> | ||
| ``` | ||
| [travis-url]: http://travis-ci.org/hemanth/gulp-hogan | ||
@@ -33,0 +102,0 @@ [travis-image]: https://travis-ci.org/hemanth/gulp-hogan.svg |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
5438
62.13%3
-25%76
52%104
197.14%1
Infinity%- Removed
- Removed