Skip to content

Commit

Permalink
cleaned up templatesystem + implemented load, include and a few more …
Browse files Browse the repository at this point in the history
…tags

- fixed require paths
- moved required statements around to prevent double loading in some modules (loader)
- organized tags, filter and nodes better so they can be changed with load tag
- general cleanup
- added load_and_render() function to loader
  • Loading branch information
Anders Hellerup Madsen committed Feb 25, 2010
1 parent fd449eb commit abad5be
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 191 deletions.
41 changes: 29 additions & 12 deletions template/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,50 @@

var sys = require('sys');
var fs = require('fs');
var template_system = require('./template');

var cache = {};
var template_path = '/tmp';

function load(name, parse_function, callback) {

// TODO: get_template
// should support subdirectories

/*
template_loader.load_and_render('template.html', test_context, function(rendered) {
dj.respond(res, rendered);
});
*/

var load = exports.load = function (name, callback) {
if (!callback) { throw 'loader.load() must be called with a callback'; }

if (cache[name] != undefined) {
callback(false, cache[name]);
} else {
fs.readFile(template_path + '/' + name, function (error, s) {
if (error) { callback(error); }
cache[name] = parse_function(s);
cache[name] = template_system.parse(s);
callback(false, cache[name]);
});
}
}

function flush() {
};

exports.load_and_render = function (name, context, callback) {
load(name, function (error, template) {
if (error) {
callback(error);
} else {
template.render(context, callback);
}
});
};

exports.flush = function () {
cache = {};
}
};

function set_path(path) {
exports.set_path = function (path) {
template_path = path;
}

exports.load = load;
exports.set_path = set_path;
exports.flush = flush;
};

37 changes: 17 additions & 20 deletions template/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
/*global require, process, exports */

var sys = require('sys');
var utils = require('utils/utils');
var template_defaults = require('template/template_defaults');
var template_loader = require('template/loader');

exports.loader = template_loader;
exports.load = function (name, callback) { template_loader.load(name, exports.parse, callback); };
var string_utils = require('../utils/string');
var html = require('../utils/html');
var iter = require('../utils/iter');

function normalize(value) {
if (typeof value !== 'string') { return value; }
Expand All @@ -31,7 +28,7 @@ function Token(type, contents) {

process.mixin(Token.prototype, {
split_contents: function () {
return utils.string.smart_split(this.contents);
return string_utils.smart_split(this.contents);
}
});

Expand Down Expand Up @@ -179,7 +176,7 @@ process.mixin(FilterExpression.prototype, {

var out = this.filter_list.reduce( function (p,c) {

var filter = template_defaults.filters[c.name];
var filter = context.filters[c.name];

var arg;
if (c.arg) {
Expand All @@ -199,9 +196,9 @@ process.mixin(FilterExpression.prototype, {

if (safety.must_escape && !safety.is_safe) {
if (typeof out === 'string') {
return utils.html.escape(out);
return html.escape(out);
} else if (out instanceof Array) {
return out.map( function (o) { return typeof o === 'string' ? utils.html.escape(o) : o; } );
return out.map( function (o) { return typeof o === 'string' ? html.escape(o) : o; } );
}
}
return out;
Expand All @@ -214,6 +211,10 @@ function Parser(input) {
this.token_list = tokenize(input);
this.indent = 0;
this.blocks = {};

var defaults = require('./template_defaults');
this.tags = defaults.tags;
this.nodes = defaults.nodes;
}

function parser_error(e) {
Expand All @@ -223,7 +224,7 @@ function parser_error(e) {
function make_nodelist() {
var node_list = [];
node_list.evaluate = function (context, callback) {
utils.iter.reduce(this, function (p, c, idx, list, next) {
iter.reduce(this, function (p, c, idx, list, next) {
c(context, function (error, result) { next(error, p + result); });
}, '', callback);
};
Expand All @@ -240,8 +241,6 @@ function make_nodelist() {

process.mixin(Parser.prototype, {

tags: template_defaults.tags,

parse: function () {

var stoppers = Array.prototype.slice.apply(arguments);
Expand All @@ -267,7 +266,7 @@ process.mixin(Parser.prototype, {
} else {
//throw parser_error('Unknown tag: ' + token[0]);
node_list.append(
template_defaults.nodes.TextNode('[[ UNKNOWN ' + token.type + ' ]]'),
this.nodes.TextNode('[[ UNKNOWN ' + token.type + ' ]]'),
'UNKNOWN'
);
}
Expand Down Expand Up @@ -302,6 +301,7 @@ function Context(o) {
this.extends = '';
this.blocks = {};
this.autoescaping = true;
this.filters = require('./template_defaults').filters;
}

process.mixin(Context.prototype, {
Expand Down Expand Up @@ -347,7 +347,7 @@ process.mixin(Context.prototype, {
},
pop: function () {
return this.scope.shift();
}
},
});


Expand All @@ -370,9 +370,8 @@ process.mixin(Template.prototype, {
if (error) { callback(error); }

if (context.extends) {
exports.load(context.extends, function (error, parent_template) {
parent_template.render(context, callback);
});
var template_loader = require('./loader');
template_loader.load_and_render(context.extends, context, callback);
} else {
callback(false, rendered);
}
Expand All @@ -383,8 +382,6 @@ process.mixin(Template.prototype, {
/********************************************************/

exports.parse = function (input) {
//var parser = new Parser(input);
// TODO: Better error handling, this is lame
return new Template(input);
};

Expand Down
4 changes: 2 additions & 2 deletions template/template.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var sys = require('sys');
process.mixin(GLOBAL, require('utils/test').dsl);
process.mixin(GLOBAL, require('template/template'));
process.mixin(GLOBAL, require('../utils/test').dsl);
process.mixin(GLOBAL, require('./template'));

testcase('Test tokenizer');
test('sanity test', function () {
Expand Down
Loading

0 comments on commit abad5be

Please sign in to comment.