Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

broccoli-handlebars

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

broccoli-handlebars - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

97

index.js

@@ -1,45 +0,51 @@

'use strict';
var path = require('path');
var fs = require('fs');
var Filter = require('broccoli-filter');
var path = require('path');
var fs = require('fs');
var Witer = require('broccoli-writer');
var Handlebars = require('handlebars');
var walkSync = require('walk-sync');
var walkSync = require('walk-sync');
var RSVP = require('rsvp');
var helpers = require('broccoli-kitchen-sink-helpers');
var mkdirp = require('mkdirp');
var Promise = RSVP.Promise;
var HandlebarsFilter = function (inputTree, options) {
var EXTENSIONS_REGEX = new RegExp('.(hbs|handlebars)');
if (!(this instanceof HandlebarsFilter)) {
return new HandlebarsFilter(inputTree, options);
var HandlebarsWiter = function (inputTree, files, options) {
if (!(this instanceof HandlebarsWiter)) {
return new HandlebarsWiter(inputTree, files, options);
}
this.inputTree = inputTree;
this.files = files;
this.options = options || {};
this.context = options.context || {};
this.handlebars = options.handlebars || Handlebars;
if (options.helpers) {
this.context = this.options.context || {};
this.handlebars = this.options.handlebars || Handlebars;
if (this.options.helpers) {
this.handlebars.registerHelper(options.helpers);
}
this.loadPartials();
};
var partials = options.partials;
HandlebarsWiter.prototype = Object.create(Witer.prototype);
HandlebarsWiter.prototype.constructor = HandlebarsWiter;
HandlebarsWiter.prototype.loadPartials = function () {
var partials = this.options.partials;
var partialsPath;
var partialsObj;
var extensionRegex;
var files;
var pertialFiles;
if (partials) {
if ('string' === typeof partials) {
extensionRegex = new RegExp('.('+this.extensions.join('|')+')');
partialsObj = {};
partialsPath = path.join(process.cwd(), partials);
files = walkSync(partialsPath).filter(extensionRegex.test.bind(extensionRegex));
if ('string' !== typeof partials) throw Error('options.partials must be a string');
files.forEach(function (file) {
var key = file.replace(partialsPath, '').replace(extensionRegex, '');
var filePath = path.join(partialsPath, file);
partialsObj[key] = fs.readFileSync(filePath).toString();
});
} else {
partialsObj = partials;
}
partialsObj = {};
partialsPath = path.join(process.cwd(), partials);
pertialFiles = walkSync(partialsPath).filter(EXTENSIONS_REGEX.test.bind(EXTENSIONS_REGEX));
pertialFiles.forEach(function (file) {
var key = file.replace(partialsPath, '').replace(EXTENSIONS_REGEX, '');
var filePath = path.join(partialsPath, file);
partialsObj[key] = fs.readFileSync(filePath).toString();
});
this.handlebars.registerPartial(partialsObj);

@@ -49,20 +55,23 @@ }

HandlebarsFilter.prototype = Object.create(Filter.prototype);
HandlebarsFilter.prototype.constructor = HandlebarsFilter;
HandlebarsFilter.prototype.extensions = ['hbs', 'handlebars'];
HandlebarsFilter.prototype.targetExtension = 'html';
HandlebarsFilter.prototype.processString = function (str, file) {
var context = this.context;
if ('function' === typeof context) {
context = context(file);
}
var template = this.handlebars.compile(str);
return template(context);
HandlebarsWiter.prototype.write = function (readTree, destDir) {
var self = this;
this.loadPartials();
return readTree(this.inputTree).then(function (sourceDir) {
var targetFiles = helpers.multiGlob(self.files, {cwd: sourceDir});
return RSVP.all(targetFiles.map(function (targetFile) {
function write (output) {
var targetHTMLFile = targetFile.replace(/(hbs|handlebars)$/, 'html');
var destFilepath = path.join(destDir, targetHTMLFile);
mkdirp.sync(path.dirname(destFilepath));
var str = fs.readFileSync(path.join(sourceDir, targetFile)).toString();
var template = self.handlebars.compile(str);
fs.writeFileSync(path.join(destDir, targetHTMLFile), template(output));
}
if ('function' !== typeof self.context) write(self.context);
return Promise.resolve(self.context(targetFile)).then(write);
}));
});
};
module.exports = HandlebarsFilter;
module.exports = HandlebarsWiter;
{
"name": "broccoli-handlebars",
"version": "0.0.1",
"version": "0.0.2",
"description": "Compile handlebars templates with helpers and dynamic contexts",

@@ -11,4 +11,4 @@ "repository": "https://github.com/moudy/broccoli-handlebars",

"keywords": [
"broccoli-plugin"
, "handlebars"
"broccoli-plugin",
"handlebars"
],

@@ -18,6 +18,9 @@ "author": "Moudy",

"dependencies": {
"broccoli-filter": "^0.1.6",
"broccoli-kitchen-sink-helpers": "^0.2.4",
"broccoli-writer": "^0.1.1",
"handlebars": "^2.0.0-alpha.4",
"mkdirp": "^0.5.0",
"rsvp": "^3.0.9",
"walk-sync": "^0.1.2"
}
}
# Broccoli Handlebars
[Broccoli](https://github.com/broccolijs/broccoli) plugin for compiling handlebars templates that supports using an existing Handlebars instance, partials, helpers, and render context based on the filename.
[Broccoli](https://github.com/broccolijs/broccoli) plugin for compiling handlebars templates that supports using an existing Handlebars instance, partials, helpers, and asynchronous render context based on the filename.

@@ -28,3 +28,3 @@ ### Install

#### context (optional)
A function or object used as the render context. The function is passed the filename allowing for dynamic contexts.
A function or object used as the render context. The function is passed the filename allowing for dynamic contexts. The function may return a value directly or a promise the resolves to a value.
```

@@ -44,4 +44,8 @@ function RenderContext () {}

context: { title: 'Foo' }
// or renter data based on the file name
context: renderContext.render.bind(renderContext)
// or return a promise
context: function () { return $.getJSON('/data.json'); }
});

@@ -70,12 +74,7 @@ ```

#### partials (optional)
Either a string that is the path to partials or an object where the key is the name of the partial and the value is the actual partial string.
A string that is the path to partials.
```
var tree = broccoliHandlebars(tree, {
partials: 'path/to/partials'
// or
partials: {
'header': '<header>foo</header'
, 'users/card': '<div>bar</div'
}
});
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc