Permalink
Browse files

Blocks works with multiple leves of extend

  • Loading branch information...
1 parent 658506a commit a59f58949433be85679addb7ca31b177922b85b1 Anders Hellerup Madsen committed Jan 15, 2010
Showing with 26 additions and 30 deletions.
  1. +7 −19 template/template.js
  2. +19 −11 template/template_defaults.js
View
@@ -5,6 +5,7 @@ var sys = require('sys');
var utils = require('utils/utils');
var template_defaults = require('template/template_defaults');
+var template_loader = require('template/loader');
/***************** TOKENIZER ******************************/
@@ -166,9 +167,7 @@ function normalize(value) {
function Context(o, blockmark) {
this.scope = [ o ];
- this.extends = [];
this.blocks = {};
- this.blockmark = blockmark;
}
process.mixin(Context.prototype, {
@@ -212,9 +211,6 @@ process.mixin(Context.prototype, {
pop: function () {
return this.scope.shift();
},
- block_placeholder: function (name) {
- return this.blockmark + name + this.blockmark;
- }
});
/*********** FilterExpression **************************/
@@ -304,25 +300,17 @@ function Template(input) {
this.node_list = parser.parse();
}
-var replace_blocks_re = /\u0000\u0000\u0000(\w+)\u0000\u0000\u0000/g
-
process.mixin(Template.prototype, {
- render: function (o, delay_blocks) {
+ render: function (o) {
- var context = (o instanceof Context) ? o : new Context(o || {}, '\u0000\u0000\u0000');
- if (!o instanceof Context)
- var context = new Context(o || {});
+ var context = (o instanceof Context) ? o : new Context(o || {});
+ context.extends = false;
var rendered = this.node_list.evaluate(context);
- if (context.extends.length > 0) {
- rendered = context.extends.pop();
- }
-
- if (!delay_blocks) {
- rendered = rendered.replace( replace_blocks_re, function (str, name) {
- return context.blocks[name];
- });
+ if (context.extends) {
+ var parent_template = template_loader.load(context.extends);
+ rendered = parent_template.render(context);
}
return rendered;
@@ -4,7 +4,6 @@
var sys = require('sys');
var template = require('template/template');
-var template_loader = require('template/loader');
var utils = require('utils/utils');
/* TODO: Missing filters
@@ -322,27 +321,36 @@ var nodes = exports.nodes = {
},
BlockNode: function (name, node_list) {
+
return function (context) {
- if (context.blocks[name]) {
- context.push({ block: { super: context.blocks[name] }});
- } else {
- context.push();
+ var out = '';
+
+ // init block list if it isn't already
+ if (!context.blocks[name]) {
+ context.blocks[name] = [];
}
- context.blocks[name] = node_list.evaluate( context );
+ // put this block in front of list
+ context.blocks[name].unshift( node_list );
+
+ context.push();
+
+ // descend through templates and evaluate for overrides
+ context.blocks[name].forEach( function (list) {
+ out = list.evaluate( context );
+ context.set('block', { super: out });
+ });
+
context.pop();
- return context.block_placeholder(name);;
+ return out;
};
},
ExtendsNode: function (item) {
return function (context) {
- var name = context.get(item);
- var parent_template = template_loader.load(name);
- var parent_rendered = parent_template.render(context, 'delay_blocks');
- context.extends.push( parent_rendered );
+ context.extends = context.get(item);
return '';
};
}

0 comments on commit a59f589

Please sign in to comment.