Permalink
Browse files

trace down and fix some rendering order problems

  • Loading branch information...
Dustin Machi
Dustin Machi committed Aug 24, 2011
1 parent c0cb9cb commit 62a707b37581be6ab3b414cdb291199a41535bd0
Showing with 76 additions and 46 deletions.
  1. +27 −10 lib/parse.js
  2. +4 −2 lib/resolver.js
  3. +33 −27 lib/rules.js
  4. +12 −7 lib/template.js
View
@@ -2,25 +2,35 @@ var when = require("promised-io/promise").when,
defer = require("promised-io/promise").defer,
lazyArray = require("commonjs-utils/lazy-array").LazyArray,
toJSON = require("commonjs-utils/json-ext").stringify,
- all = require("promised-io/promise").all;
+ all = require("promised-io/promise").all,
+ t = require("templify/template"),
+ extendTemplate = require("./template").extendTemplate,
+ Template = require("./template").Template;
+
+
exports.parse = function(source,state,options){
+ //console.log("parse source: ", source,"\n\n\n\n");
var functionText,
srcArray= exports.toArray(source,state,options.rules);
source = (typeof source=='array')?source:[source];
return when(srcArray, function(sourceArray){
if ((!sourceArray.tags) || (sourceArray.tags.length<1)){
+// console.log("parse source: ", source);
return ["_write(", source, ");"].join('');
}else{
+// console.log("to tree");
var sourceTree = exports.toTree(sourceArray.all, state, options.rules);
+// console.log("to script");
functionText = exports.toScript(sourceTree, escapeChars, state,options.rules);
-
+ //console.log("functionText: ", functionText);
if (state._extend){
- //print("perform _extend: ");
- lazyScriptParts = extendTemplate(state,options.rules);
- //print("EXTENDED FUNCTION TEXT: " + this.functionText);
+// console("perform _extend: ");
+ return lazyScriptParts = t.extendTemplate(state,options);
+// console("EXTENDED FUNCTION TEXT: " + this.functionText);
}
+
}
return functionText;
});
@@ -46,8 +56,13 @@ exports.toArray= function(src, state, rules) {
var text = [],
tags = [],
all = [];
- return when(src.forEach(function(source){
- source = source.decodeToString ? source.decodeToString() : '' + source;
+ //console.log("src: ", src)
+ if (!src){return {text: text, tags: tags, all: all}}
+ var source="";
+ return when(src.forEach(function(s){
+ source += s.decodeToString ? s.decodeToString() : '' + s;
+ }),function(){
+ //console.log("source: ", source);
while(source) {
for(var i in rules) {
if(!delimiter || source.indexOf(rules[delimiter].opener) == -1)
@@ -75,8 +90,9 @@ exports.toArray= function(src, state, rules) {
all.push(tags[i]);
}
}
-
- }), function(){return {text: text, tags: tags, all: all}});
+ return {text: text, tags: tags, all: all}
+ });
+
};
exports.parseArguments = function(source, expr) {
@@ -195,7 +211,7 @@ exports.toTree = function(array, state, rules) {
addChild(current, obj);
}
}
-
+ //console.log("doc:",doc);
return doc;
};
@@ -215,6 +231,7 @@ exports.toScript = function(tree, list, state, rules) {
});
}
return when(all(content), function(content){
+ //console.log("toScript() content: ", content);
content=content.join('');
if(!tree.isDocument) {
if(tree.tagname){
View
@@ -12,15 +12,17 @@ var when = require("promised-io/promise").when;
exports.Resolver = function(store,options) {
var defaultTemplate = require("commonjs-utils/settings").defaultTemplate || "default"
return function(id,exact){
- //print("Resolve Template for ID: " + id);
+ console.log("Resolve Template for ID: " + id);
var template,
parts=id.split("/");
+ console.log("pars: ", parts);
while(parts.length>0){
var tid = parts.join("/");
var templateString="";
+ console.log ("tid: ", tid);
template = store.get(tid);
if (template){
- //print("Found Template: " + tid);
+ console.log("Found Template: " + tid);
return template;
}
if (exact){return}
View
@@ -17,14 +17,14 @@ exports.Rules = function(options){
type: 'single',
handler: function(tree, content, caller){
var templateName = tree.arguments.template;
- print("DO EXTEND: " + templateName);
+ //console.log("DO EXTEND: " + templateName);
caller._extend = {
blocks: {},
templateName: templateName
}
if (caller.extension){
- print("found caller.extentsion");
+ //console.log("found caller.extentsion");
for (var i in caller.extension.blocks){
caller._extend.blocks[i] = caller.extension.blocks[i];
}
@@ -39,32 +39,32 @@ exports.Rules = function(options){
type: 'block',
handler: function(tree, content, caller){
var blockName = tree.arguments.blockName;
- //print("BLOCK: " + blockName);
- //print(" _subclass blocks: " + caller.extension[blockName]);
+ //console.log("BLOCK: " + blockName);
+ //console.log(" _subclass blocks: " + caller.extension[blockName]);
if (!blockName){
throw new Error("Ivalid blockName >" + blockName + "<");
}
if (caller.extension && caller.extension.blocks[blockName]){
- //print(" current extended block: " + caller.extension.blocks[blockName]);
+ //console.log(" current extended block: " + caller.extension.blocks[blockName]);
if (caller._extend){
- //print(" Extending Again, use previous if available");
+ //console.log(" Extending Again, use previous if available");
caller._extend.blocks[blockName]=caller.extension.blocks[blockName];
- //print(" From extension: " + caller._extend.blocks[blockName]);
+ //console.log(" From extension: " + caller._extend.blocks[blockName]);
}// else{
- //print("returning _extended block");
- //print(" From extension: " + caller.extension.blocks[blockName]);
+ //console.log("returning _extended block");
+ //console.log(" From extension: " + caller.extension.blocks[blockName]);
return caller.extension.blocks[blockName];
//}
}else if (caller._extend){
- //print("Add extended block " + blockName);
+ //console.log("Add extended block " + blockName);
caller._extend.blocks[blockName]=content;
return caller._extend.blocks[blockName];
- //print(" added block: " + caller._extend.blocks[blockName]);
+ //console.log(" added block: " + caller._extend.blocks[blockName]);
}else{
- //print("Returning block content directly");
- //print(" Direct: " + content);
+ //console.log("Returning block content directly");
+ //console.log(" Direct: " + content);
return content;
}
}
@@ -74,12 +74,12 @@ exports.Rules = function(options){
arguments: '{template}',
type: 'single',
handler: function(tree, content, caller){
- //print("templateName: " + tree.arguments.template);
var templateName = tree.arguments.template;
var template = Template(resolver(templateName), options, true);
- //print("Do IMPORT of template: " + templateName + ":" + template);
+// console.log("Do IMPORT of template: " + templateName + ":" + template);
return when(template, function(template){
+// console.log("included template: ", template);
return template;
});
}
@@ -92,13 +92,13 @@ exports.Rules = function(options){
var templateName = tree.arguments.template;
var rootContext = tree.arguments.rootContext;
var templateString = resolver(templateName, true);
- //print("Do RENDER of template: " + templateName + ": " + templateString);
+ //console.log("Do RENDER of template: " + templateName + ": " + templateString);
if (rootContext){
var ret = ["with(",rootContext,"){",Template(templateString, options, true), "};"].join('');
- //print("Rendered Function Text with rootContext: " + ret);
+ //console.log("Rendered Function Text with rootContext: " + ret);
return ret;
}
- //print("Rendered rootless functionText: " + subparser.functionText);
+ //console.log("Rendered rootless functionText: " + subparser.functionText);
return Template(templateString,options);
}
},
@@ -109,16 +109,20 @@ exports.Rules = function(options){
handler: function(tree, content, caller) {
var element = tree.arguments.element;
var object = tree.arguments.object;
- //print("serialized tree arguments: " + serialize(tree));
+ //console.log("serialized tree arguments: " + serialize(tree));
//Check whether there are else tag after this tag, if yes, (if) tag will be included
//var cond = (tree.parent.children[tree.nr+1] && tree.parent.children[tree.nr+1].tagname == 'else');
//var iff = ['if( (',object,' instanceof Array && ',object,'.length > 0) || ',
// '(!(',object,' instanceof Array) && ',object,') ) {'].join('');
+
+ var buf=[];
+ //console.log("Foreach object: ", object, element);
return [
- 'var x =', object,';',
+ 'var x=',object,';',
'x.forEach(function(', element,'){',
- //'print("serialized body item: " + serialize(', element, '));',
+// 'console.log("serialized body item: " + serialize(', element, '));',
//'var ', element, '=el;',content,
+ //'console.log("item: ",',element,')',
content,
'});'
].join('');
@@ -138,11 +142,11 @@ exports.Rules = function(options){
// '(!(',object,' instanceof Array) && ',object,') ) {'].join('');
return [
'for(var i in ', object, '){',
- //'print("i: " + i + " " + ', object, '[i]);',
+ //'console.log("i: " + i + " " + ', object, '[i]);',
'var ', element, '=',object,'[i];',
- //'print("element: " + ',element,');',
- //'print("serialized element: " + serialize(', element,'));',
- //'print(".name " + ', element, '.name);',
+ //'console.log("element: " + ',element,');',
+ //'console.log("serialized element: " + serialize(', element,'));',
+ //'console.log(".name " + ', element, '.name);',
content,
'}'
].join('');
@@ -154,8 +158,9 @@ exports.Rules = function(options){
handler: function(tree, content, caller) {
var condition = tree.argSource;
+ //console.log("if block: ", condition, "content:", content,"****");
return [
- 'if(',condition,') {',
+ 'if(',condition,'){',
content,
'}'
].join('');
@@ -224,14 +229,15 @@ exports.Rules = function(options){
opener: '${',
closer: '}}',
handler: function(tree, content, caller) {
- return '_write('+tree.argSource+');';
+ return 'if(' +tree.argSource+'!= undefined){_write('+tree.argSource+')}';
}
},
alternatePrint: {
opener: '{{',
closer: '}}',
handler: function(tree, content, caller) {
+ return 'if(' +tree.argSource+'){_write('+tree.argSource+')}';
return '_write('+tree.argSource+');';
}
},
View
@@ -15,7 +15,7 @@ var Template = exports.Template = function(source, options, noWrapper){
options.rules=options.rules || require("./rules").Rules(options);
if (options.extension){
- state.extension = options.extension ;
+ state.extension = options.extension;
}
// header for compiled template function
@@ -27,18 +27,23 @@ var Template = exports.Template = function(source, options, noWrapper){
return when(source, function(source){
var parsed = parse(source, state, options);
return when(parsed, function(parsed){
+
if (!noWrapper){
var wrapped = [options.header,parsed, options.footer].join('');
- return new Function('$data', wrapped);
- }else{
- return parsed;
+ //console.log("when parsed: ", wrapped);
+ parsed = new Function("$data", wrapped);
}
+
+ return parsed;
});
});
}
-var extendTemplate=function(state,options){
- var templateString = options.resolver(state.extension.templateName, true)
- return Template(templateString, options, state.extension, true);
+exports.extendTemplate = function(state,options){
+ if (state._extend){
+ options.extension=state._extend;
+ }
+ var templateString = options.resolver(options.extension.templateName, true)
+ return Template(templateString, options, true);
};

0 comments on commit 62a707b

Please sign in to comment.