Skip to content

Commit

Permalink
Changed template engine variable name to allow direct output from hel…
Browse files Browse the repository at this point in the history
…per functions.
  • Loading branch information
cskr committed Aug 25, 2010
1 parent 62f5989 commit f37b017
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,3 @@ Hashes containing the necessary dependencies can be added to the `this` context
});

gh.serve(8080);

To Do
-----

* Improve template engine to allow callbacks in helpers.
12 changes: 6 additions & 6 deletions grasshopper/lib/ghp.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports.addHelpers = function(newHelpers) {
}

function compile(text, helpersCount) {
var funcBody = "var p=[], model = model || {};";
var funcBody = "var _out = [], model = model || {};";

for(var i = 0; i < helpersCount; i++) {
funcBody += "with(helpers[" + i + "]) {";
Expand All @@ -47,18 +47,18 @@ function compile(text, helpersCount) {
var parts = text.split("<%");
parts.forEach(function(part) {
if(part.indexOf("%>") == -1) {
funcBody += "p.push('" + escapeCode(part) + "');";
funcBody += "_out.push('" + escapeCode(part) + "');";
} else if(part.charAt(0) == '=') {
var subParts = part.split('%>');
funcBody += "p.push(" + subParts[0].substring(1) + ");";
funcBody += "_out.push(" + subParts[0].substring(1) + ");";
if(subParts.length > 1) {
funcBody += "p.push('" + escapeCode(subParts[1]) + "');";
funcBody += "_out.push('" + escapeCode(subParts[1]) + "');";
}
} else {
var subParts = part.split('%>');
funcBody += subParts[0];
if(subParts.length > 1) {
funcBody += "p.push('" + escapeCode(subParts[1]) + "');";
funcBody += "_out.push('" + escapeCode(subParts[1]) + "');";
}
}
});
Expand All @@ -70,7 +70,7 @@ function compile(text, helpersCount) {
funcBody += "}";
}

funcBody += "return p.join('');"
funcBody += "return _out.join('');"
return new Function("model", "helpers", funcBody);
}

Expand Down
3 changes: 3 additions & 0 deletions grasshopper/lib/grasshopper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var http = require('http'),
multipart = require('./multipart'),
session = require('./session'),
model = require('./model'),
ghp = require('./ghp'),
i18n = require('./i18n');

process.on('uncaughtException', function(err) {
Expand Down Expand Up @@ -50,6 +51,8 @@ exports.addFilters = function(regex) {
filters.push({pattern: regex, filters: filtersArray});
}

exports.addHelpers = ghp.addHelpers;

exports.configure = function(config) {
renderer.configure(config);
multipart.configure(config);
Expand Down

0 comments on commit f37b017

Please sign in to comment.