Skip to content
Browse files

first pass at helpers/dynamic helpers support...

  • Loading branch information...
1 parent c3f1269 commit 99dc50ab84f38a9dcd63d43062805c11c82bae73 @fat committed Jul 4, 2011
Showing with 63 additions and 27 deletions.
  1. +16 −2 examples/app.js
  2. +2 −1 examples/views/index.mustache
  3. +45 −24 lib/stache.js
View
18 examples/app.js
@@ -2,13 +2,27 @@
var express = require('express')
, app = express.createServer();
-//config
+// config
app.set('view engine', 'mustache')
app.set("views", __dirname + '/views');
app.register(".mustache", require('stache'));
app.use(express.static(__dirname + '/public'));
-//routes
+// helpers
+app.helpers({
+ helloworld: function(req, res){
+ return 'hello world';
+ }
+});
+
+// dynamicHelpers
+app.dynamicHelpers({
+ hellopage: function(req, res){
+ return req.url;
+ }
+});
+
+// routes
app.get('/', function (req, res) {
res.render('index', {
locals: {
View
3 examples/views/index.mustache
@@ -1,2 +1,3 @@
-Hellllllo world. yeah.
+{{helloworld}}!!! You are currently viewing: {{hellopage}}!
+
{{>img}}
View
69 lib/stache.js
@@ -5,44 +5,65 @@ var stache = {}
, fs = require('fs')
, path = require("path")
, mustache = require('mustache')
- , views = '/';
+ , views = './views'
+ , union = function(source){
+ var object
+ , key
+ , l
+ , i
+
+ for (i = 1, l = arguments.length; i < l; i++){
+ object = arguments[i]
+ for (key in object) {
+ if (!source.hasOwnProperty(key)) {
+ source[key] = object[key]
+ }
+ }
+ }
+ return source
+}
stache.compile = function (source, options) {
- views = (options && options.settings && options.settings.views) || './views';
+ views = (options && options.settings && options.settings.views) || views
+
if (typeof source == 'string') {
return function (options) {
- options.locals = options.locals || {};
- options.partials = options.partials || {};
+ var partials_regex = new RegExp("{{([>-])([^\\/#\\^]+?)\\1?}}+", "g")
+ , lines = source.split("\n")
+ , i = 0
+ , view
- if (options.body) {
- options.locals.yield = options.body;
- }
-
- var partials_regex = new RegExp("{{([>-])([^\\/#\\^]+?)\\1?}}+", "g");
-
- var tag_replace_callback = function (match, operator, name) {
+ function tag_replace_callback(match, operator, name) {
if (operator == '>' && !options.partials[name]) {
- var partialFileName = views + '/' + name + (options.extension || '.mustache');
- return path.existsSync(partialFileName) ? fs.readFileSync(partialFileName, "utf-8") : "";
+ var partialFileName = views + '/' + name + (options.extension || '.mustache')
+ return path.existsSync(partialFileName) ? fs.readFileSync(partialFileName, "utf-8") : ""
}
- return match;
- };
+ return match
+ }
- var lines = source.split("\n");
- for (var i = 0; i < lines.length; i++) {
- lines[i] = lines[i].replace(partials_regex, tag_replace_callback, this);
+ options.locals = options.locals || {}
+ options.partials = options.partials || {}
+
+ if (options.body) {
+ options.locals.yield = options.body
+ }
+
+ for (i; i < lines.length; i++) {
+ lines[i] = lines[i].replace(partials_regex, tag_replace_callback, this)
}
- return mustache.to_html(lines.join('\n'), options.locals, options.partials);
+ view = union({}, options, options.locals)
+
+ return mustache.to_html(lines.join('\n'), view, options.partials)
}
} else {
- return source;
+ return source
}
};
stache.render = function(template, options) {
- template = stache.compile(template, options);
- return template(options);
-};
+ template = stache.compile(template, options)
+ return template(options)
+}
-module.exports = stache;
+module.exports = stache

0 comments on commit 99dc50a

Please sign in to comment.
Something went wrong with that request. Please try again.