Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix some issues with if/else, wrap template in try catch to keep from…

… crashing when a template is incorrect, add set function
  • Loading branch information...
commit 44dba427db98529435e8c661e9c8b1280d4ee9df 1 parent fc0dbfb
Dustin Machi authored
Showing with 30 additions and 11 deletions.
  1. +1 −2  lib/resolver.js
  2. +27 −6 lib/rules.js
  3. +2 −3 lib/template.js
3  lib/resolver.js
View
@@ -15,11 +15,10 @@ exports.Resolver = function(store,options) {
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);
+ //console.log ("tid: ", tid);
template = store.get(tid);
if (template){
console.log("Found Template: " + tid);
33 lib/rules.js
View
@@ -116,6 +116,16 @@ exports.Rules = function(options){
return Template(templateString,options);
}
},
+
+ 'set': {
+ arguments: '{variable} to {args}',
+ type: 'single',
+ handler: function(tree, content, caller){
+ var variable = tree.arguments.variable;
+ var args= tree.arguments.args;
+ return 'console.log("data:",$data);$data["'+variable+'"]=' + args + ";";
+ }
+ },
'foreach': {
arguments: '{element} in {object}',
@@ -138,7 +148,7 @@ exports.Rules = function(options){
//'var ', element, '=el;',content,
//'console.log("item: ",',element,')',
content,
- '});'
+ '},this);'
].join('');
}
},
@@ -183,7 +193,7 @@ exports.Rules = function(options){
'elseif': {
type: 'block',
- noTextBefore: true,
+ noTextBefore: false,
handler: function(tree, content, caller) {
var condition = tree.argSource;
@@ -198,9 +208,9 @@ exports.Rules = function(options){
'else': {
type: 'block',
- noTextBefore: true,
-
+ noTextBefore: false,
handler: function(tree, content, caller) {
+ console.log("else handler: ", content);
return [
'else {',
content,
@@ -229,6 +239,16 @@ exports.Rules = function(options){
].join('');
}
},
+
+ 'log': {
+ arguments: '{prefix} {obj}',
+ type: "single",
+ handler: function(tree, content, caller){
+ var prefix = tree.arguments.prefix;
+ var obj= tree.arguments.obj;
+ return 'console.log("' + prefix + '",' + obj + ');'
+ }
+ },
'cdata': {
type: 'block',
@@ -251,8 +271,9 @@ exports.Rules = function(options){
opener: '{{',
closer: '}}',
handler: function(tree, content, caller) {
- return 'if(' +tree.argSource+'){_write('+tree.argSource+')}';
- return '_write('+tree.argSource+');';
+ //return 'if(' +tree.argSource+'!= undefined){_write('+tree.argSource+')}';
+ //return '_write('+tree.argsource + " || '');";
+ return '_write(('+tree.argSource+'||"").toString());';
}
},
5 lib/template.js
View
@@ -19,10 +19,10 @@ var Template = exports.Template = function(source, options, noWrapper){
}
// header for compiled template function
- options.header = 'return { forEach: function(_write) { with($data){';
+ options.header = 'return { forEach: function(_write){try{with($data){';
// footer for compiled template function
- options.footer = '}}}';
+ options.footer = '}}catch(err){console.log("Template Error: "+err)}}}';
return when(source, function(source){
var parsed = parse(source, state, options);
@@ -31,7 +31,6 @@ var Template = exports.Template = function(source, options, noWrapper){
var wrapped = [options.header,parsed, options.footer].join('');
parsed = new Function("$data", wrapped);
}
-
return parsed;
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.