Permalink
Browse files

Fix require statements to avoid duplicate loading

  • Loading branch information...
1 parent 87c7718 commit e889bcfa175e380d629ffd3f8024e8a04758c0a5 Anders Hellerup Madsen committed Feb 15, 2010
Showing with 29 additions and 17 deletions.
  1. +20 −7 template/loader.js
  2. +4 −2 template/template.js
  3. +2 −3 template/template_defaults.tags.test.js
  4. +3 −5 template_example.js
View
@@ -3,19 +3,32 @@
var sys = require('sys');
var posix = require('posix');
-var template = require('template/template');
var cache = {};
-var template_path = '.';
+var template_path = '/tmp';
-function load(name) {
- if (!cache[name]) {
- var content = posix.cat(template_path + '/' + name).wait();
- cache[name] = template.parse(content);
+function load(name, parse_function, callback) {
+ if (cache[name] != undefined) {
+ if (callback) {
+ callback(cache[name]);
+ } else {
+ return cache[name];
+ }
+ } else {
+ if (callback) {
+ posix.cat(template_path + '/' + name).addCallback(function(s) {
+ cache[name] = parse_function(s);
+ callback(cache[name]);
+ });
+ } else {
+ var content = posix.cat(template_path + '/' + name).wait();
+ cache[name] = parse_function(content);
+ return cache[name];
+ }
}
- return cache[name];
}
+
function flush() {
cache = {};
}
@@ -2,11 +2,12 @@
/*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) { return template_loader.load(name, exports.parse, callback); };
function normalize(value) {
if (typeof value !== 'string') { return value; }
@@ -318,6 +319,7 @@ process.mixin(Context.prototype, {
val = this.scope[level][name];
while (parts.length && val) {
next = val[parts.shift()];
+
if (typeof next === 'function') {
val = next.apply(val);
} else {
@@ -363,7 +365,7 @@ process.mixin(Template.prototype, {
var rendered = this.node_list.evaluate(context);
if (context.extends) {
- var parent_template = template_loader.load(context.extends);
+ var parent_template = exports.load(context.extends);
rendered = parent_template.render(context);
}
@@ -1,7 +1,6 @@
var sys = require('sys');
var posix = require('posix');
var template = require('template/template');
-var template_loader = require('template/loader');
process.mixin(GLOBAL, require('utils/test').dsl);
process.mixin(GLOBAL, require('template/template_defaults'));
@@ -96,8 +95,8 @@ testcase('filter')
testcase('block and extend')
setup(function () {
- template_loader.flush();
- template_loader.set_path('/tmp');
+ template.loader.flush();
+ template.loader.set_path('/tmp');
})
test('block should parse and evaluate', function () {
View
@@ -2,11 +2,9 @@ var posix = require('posix'),
sys = require('sys'),
dj = require('djangode'),
template_system = require('template/template');
- template_loader = require('template/loader');
-
// set template path
-template_loader.set_path('template-demo');
+template_system.loader.set_path('template-demo');
// context to use when rendering template. In a real app this would likely come from a database
var test_context = {
@@ -28,12 +26,12 @@ var app = dj.makeApp([
['^/(template-demo/.*)$', dj.serveFile],
['^/template$', function (req, res) {
- var html = template_loader.load('template.html').render(test_context);
+ var html = template_system.load('template.html').render(test_context);
dj.respond(res, html);
}],
['^/text$', function (req, res) {
- var html = template_loader.load('template.html').render(test_context);
+ var html = template_system.load('template.html').render(test_context);
dj.respond(res, html, 'text/plain');
}]
]);

0 comments on commit e889bcf

Please sign in to comment.