Skip to content

Loading…

custom dustjs-herlpers support #126

Open
wants to merge 3 commits into from

3 participants

@tiefenb

Support for custom dustjs helpers over express-js options/settings.

see the example for usage:
https://github.com/tiefenb/consolidate.js#custom-dustjs-helpers

Markus Tiefe... added some commits
@RobLoach RobLoach commented on the diff
package.json
@@ -41,7 +41,7 @@
"main": "index",
"repository": {
"type": "git",
- "url": "https://github.com/visionmedia/consolidate.js.git"
+ "url": "https://github.com/tiefenb/consolidate.js.git"

This change doesn't look desired.

@tiefenb
tiefenb added a note

yes, can you drop this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@BridgeAR BridgeAR commented on the diff
Readme.md
((17 lines not shown))
+app.set('views', __dirname + '/views');
+app.set('template_engine', 'dust');
+
+var helpers = {};
+
+/* dust helper for date formating with momentjs */
+helpers.moment = function(chunk, context, bodies, params) {
+ var format = dust.helpers.tap(params.format, chunk, context);
+ var value = dust.helpers.tap(params.value, chunk, context);
+ var output = moment(value).format(format);
+ return chunk.write(output);
+};
+
+app.set('helpers', helpers);
+```
+
@BridgeAR Collaborator

It is not desirable to include documentation from the included libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@BridgeAR
Collaborator

Would you be so kind and rebase this and remove the documentation? I guess the documentation is already available in the main lib documentation, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 9, 2013
  1. support for custom dustjs-helpers

    Markus Tiefenbacher committed
  2. Readme Update with custom dustjs-helper example

    Markus Tiefenbacher committed
  3. package json for npm

    Markus Tiefenbacher committed
Showing with 44 additions and 9 deletions.
  1. +29 −0 Readme.md
  2. +12 −6 lib/consolidate.js
  3. +3 −3 package.json
View
29 Readme.md
@@ -117,6 +117,35 @@ app.listen(3000);
console.log('Express server listening on port 3000');
```
+## Custom dustjs-helpers
+
+### Example for custom dustjs-helpers:
+
+```js
+
+var express = express('express');
+var moment = require('moment');
+var cons = require('consolidate');
+var app = express();
+
+app.engine('dust', cons.dust);
+app.set('view engine', 'dust');
+app.set('views', __dirname + '/views');
+app.set('template_engine', 'dust');
+
+var helpers = {};
+
+/* dust helper for date formating with momentjs */
+helpers.moment = function(chunk, context, bodies, params) {
+ var format = dust.helpers.tap(params.format, chunk, context);
+ var value = dust.helpers.tap(params.value, chunk, context);
+ var output = moment(value).format(format);
+ return chunk.write(output);
+};
+
+app.set('helpers', helpers);
+```
+
@BridgeAR Collaborator

It is not desirable to include documentation from the included libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
## Running tests
Install dev deps:
View
18 lib/consolidate.js
@@ -93,8 +93,6 @@ function read(path, options, fn) {
// read
fs.readFile(path, 'utf8', function(err, str){
if (err) return fn(err);
- // remove extraneous utf8 BOM marker
- str = str.replace(/^\uFEFF/, '');
if (options.cache) readCache[path] = str;
fn(null, str);
});
@@ -211,8 +209,16 @@ exports.dust.render = function(str, options, fn){
if (options) {
if (options.ext) ext = options.ext;
- if (options.views) views = options.views;
- if (options.settings && options.settings.views) views = options.settings.views;
+ if (options.views) { views = options.views; }
+ if(options.settings) {
+ if (options.settings.helpers && engine.helpers) {
+ helpers = options.settings.helpers;
+ Object.keys(helpers).forEach(function(key) {
+ engine.helpers[key] = requires.dust.helpers[key] = helpers[key];
+ });
+ }
+ if (options.settings.views) views = options.settings.views;
+ }
}
if (!options || (options && !options.cache)) engine.cache = {};
@@ -679,7 +685,7 @@ exports.toffee = function(path, options, fn){
exports.toffee.render = function(str, options, fn) {
var engine = requires.toffee || (requires.toffee = require('toffee'));
try {
- engine.str_render(str, options,fn);
+ engine.str_render(str, options,fn);
} catch (err) {
fn(err);
}
@@ -698,7 +704,7 @@ exports.dot = fromStringRenderer('dot');
exports.dot.render = function (str, options, fn) {
var engine = requires.dot || (requires.dot = require('dot'));
try {
- var tmpl = cache(options) || cache(options, engine.compile(str, options && options._def));
+ var tmpl = cache(options) || cache(options, engine.compile(str));
fn(null, tmpl(options));
} catch (err) {
fn(err);
View
6 package.json
@@ -23,8 +23,8 @@
"haml-coffee": "1.4.0",
"hogan.js": "2.0.0",
"dust": "0.3.0",
- "dustjs-linkedin": "1.2",
- "dustjs-helpers":"1.1.1",
+ "dustjs-linkedin": ">=2.0.3",
+ "dustjs-helpers":">=1.1.1",
"handlebars": "1.0.7",
"underscore": "1.3.3",
"qejs": "0.0.1",
@@ -41,7 +41,7 @@
"main": "index",
"repository": {
"type": "git",
- "url": "https://github.com/visionmedia/consolidate.js.git"
+ "url": "https://github.com/tiefenb/consolidate.js.git"

This change doesn't look desired.

@tiefenb
tiefenb added a note

yes, can you drop this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
},
"scripts": {
"test": "mocha"
Something went wrong with that request. Please try again.