Permalink
Browse files

Created all engine wrappers, and edited Templato so we can send in en…

…gine settings(Like caching, etc)
  • Loading branch information...
1 parent 44bc33e commit 422b6f254a0f288916eb93e9d78db30383f01877 @larzconwell larzconwell committed Jun 17, 2012
View
26 deps/ejs.js
@@ -46,7 +46,7 @@ ejs.Template = function(params) {
ejs.Template.prototype = new function() {
var _REGEX = /(<%%)|(%%>)|(<%=)|(<%-)|(<%#)|(<%)|(%>)|(-%>)/;
- this.modes = {
+ exports.modes = {
EVAL: 'eval'
, ESCAPED: 'escaped'
, RAW: 'raw'
@@ -186,7 +186,7 @@ ejs.Template.prototype = new function() {
this.parseTemplateText = function() {
var str = this.templateText
- , pat = _REGEX
+ , pat = exports._REGEX
, result = pat.exec(str)
, arr = []
, firstPos
@@ -238,24 +238,24 @@ ejs.Template.prototype = new function() {
switch (line) {
case '<%':
- this.mode = this.modes.EVAL;
+ this.mode = exports.modes.EVAL;
break;
case '<%=':
- this.mode = this.modes.ESCAPED;
+ this.mode = exports.modes.ESCAPED;
break;
case '<%-':
- this.mode = this.modes.RAW;
+ this.mode = exports.modes.RAW;
break;
case '<%#':
- this.mode = this.modes.COMMENT;
+ this.mode = exports.modes.COMMENT;
break;
case '<%%':
- this.mode = this.modes.LITERAL;
+ this.mode = exports.modes.LITERAL;
this.source += '__output += "' + line.replace('<%%', '<%') + '";';
break;
case '%>':
case '-%>':
- if(this.mode == this.modes.LITERAL) _addOutput();
+ if(this.mode == exports.modes.LITERAL) _addOutput();
this.mode = null;
this.truncate = line.indexOf('-') == 0;
@@ -265,25 +265,25 @@ ejs.Template.prototype = new function() {
if (this.mode) {
switch (this.mode) {
// Just executing code
- case this.modes.EVAL:
+ case exports.modes.EVAL:
this.source += line;
break;
// Exec, esc, and output
- case this.modes.ESCAPED:
+ case exports.modes.ESCAPED:
// Add the exec'd, escaped result to the output
this.source += '__output += utils.string.escapeXML(' +
line.replace(/;\S*/, '') + ');';
break;
// Exec and output
- case this.modes.RAW:
+ case exports.modes.RAW:
// Add the exec'd result to the output
this.source += '__output += ' + line + ';';
break;
- case this.modes.COMMENT:
+ case exports.modes.COMMENT:
// Do nothing
break;
// Literal <%% mode, append as raw output
- case this.modes.LITERAL:
+ case exports.modes.LITERAL:
_addOutput();
break;
}
View
4 deps/templato/lib/engines/ejs.js
@@ -8,8 +8,8 @@ var TemplatoEJS = (function() {
}
};
- TemplatoEJS.prototype.compile = function(template) {
- return this.engine.compile(template);
+ TemplatoEJS.prototype.compile = function(template, options) {
+ return this.engine.compile(template, options);
};
TemplatoEJS.prototype.render = function(data, fn) {
View
4 deps/templato/lib/engines/geddy_ejs.js
@@ -6,8 +6,8 @@ var TemplatoGeddyEJS = (function() {
this.engine = this.engine || require('../../../ejs');
};
- TemplatoGeddyEJS.prototype.compile = function(template) {
- return this.engine.compile(template);
+ TemplatoGeddyEJS.prototype.compile = function(template, options) {
+ return this.engine.compile(template, options);
};
TemplatoGeddyEJS.prototype.render = function(data, fn) {
View
4 deps/templato/lib/engines/handlebars.js
@@ -8,8 +8,8 @@ var TemplatoHandlebars = (function() {
}
};
- TemplatoHandlebars.prototype.compile = function(template) {
- return this.engine.compile(template);
+ TemplatoHandlebars.prototype.compile = function(template, options) {
+ return this.engine.compile(template, options);
};
TemplatoHandlebars.prototype.render = function(data, fn) {
View
4 deps/templato/lib/engines/jade.js
@@ -8,8 +8,8 @@ var TemplatoJade = (function() {
}
};
- TemplatoJade.prototype.compile = function(template) {
- return this.engine.compile(template);
+ TemplatoJade.prototype.compile = function(template, options) {
+ return this.engine.compile(template, options);
};
TemplatoJade.prototype.render = function(data, fn) {
View
4 deps/templato/lib/engines/mustache.js
@@ -14,8 +14,8 @@ var TemplatoHandlebars = (function() {
}
};
- TemplatoHandlebars.prototype.compile = function(template) {
- return this.engine.compile(template);
+ TemplatoHandlebars.prototype.compile = function(template, options) {
+ return this.engine.compile(template, options);
};
TemplatoHandlebars.prototype.render = function(data, fn) {
View
3 deps/templato/lib/templato.js
@@ -33,6 +33,7 @@ Templato = (function() {
Templato.prototype.set = function(opts) {
opts = opts || {};
+ this.options = opts.options || {};
this.engine = (function() {
switch(opts.engine) {
@@ -70,7 +71,7 @@ Templato = (function() {
Templato.prototype.compile = function() {
if(this.fn) return this.fn;
- return this.fn = this.engine.compile(this.template);
+ return this.fn = this.engine.compile(this.template, this.options);
};
Templato.prototype.render = function(data) {
View
9 lib/template/adapters/ejs.js
@@ -114,18 +114,21 @@ ejs.Template.prototype = new function() {
this.process = function(params) {
params = params || {}
- var domNode = params.node;
+ var domNode = params.node
+ , settings;
this.data = params;
this.source = this.source || ''; // Cache generated template for speed
// If no cache is available generate template
//if(!this.source) this.source = this.generateSource();
if(!this.source) {
- this.templato.set({
+ settings = {
engine: 'geddy_ejs'
, template: this.templateText
- });
+ };
+
+ this.templato.set(settings);
this.source = this.templato.render(this.data);
}
View
301 lib/template/adapters/ejs_engine_copy.js
@@ -1,301 +0,0 @@
-/*
- * Geddy JavaScript Web development framework
- * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
-*/
-
-var utils = require('../../utils')
- , ejs = {};
-
-ejs.Template = function(params) {
- params = params || {};
-
- this.mode = null;
- this.truncate = false;
- this.currentLine = 1;
- this.afterLoaded = params.afterLoaded;
- this.source = '';
- this.markup = undefined;
- // If you don't want to use Fleegix.js,
- // override getTemplateTextFromNode to use
- // textarea node value for template text
- this.templateText = params.text || this.getTemplateTextFromNode(params.node);
-
- // Try to get from URL
- if(typeof this.templateText == 'undefined') {
- // If you don't want to use Fleegix.js,
- // override getTemplateTextFromUrl to use
- // files for template text
- this.getTemplateTextFromUrl(params);
- }
-};
-
-ejs.Template.prototype = new function() {
- var _REGEX = /(<%%)|(%%>)|(<%=)|(<%-)|(<%#)|(<%)|(%>)|(-%>)/;
-
- this.modes = {
- EVAL: 'eval'
- , ESCAPED: 'escaped'
- , RAW: 'raw'
- , APPEND: 'append'
- , COMMENT: 'comment'
- , LITERAL: 'literal'
- };
-
- this.getTemplateTextFromNode = function(node) {
- // Requires the fleegix.xhr module
- if(typeof fleegix.string == 'undefined') throw('Requires fleegix.string module.');
- var ret;
-
- if(node) {
- ret = node.value;
- ret = fleegix.string.unescapeXML(ret);
- ret = fleegix.string.trim(ret);
- }
- return ret;
- };
-
- this.getTemplateTextFromUrl = function(params) {
- // Requires the fleegix.xhr module
- if(typeof fleegix.xhr == 'undefined') throw('Requires fleegix.xhr module.');
- var self = this
- , url = params.url
- , noCache = params.preventCache || false;
-
- // Found text in cache, and caching is turned on
- if(text && !noCache) {
- this.templateText = text;
- }
- // Otherwise go grab the text
- else {
- var callback
- , opts;
-
- // Callback for setting templateText and caching --
- // used for both sync and async loading
- callback = function (s) {
- self.templateText = s;
- ejs.templateTextCache[url] = s;
-
- // Use afterLoaded hook if set
- if(typeof self.afterLoaded == 'function') {
- self.afterLoaded();
- }
- };
-
- if(params.async) {
- opts = {
- url: url
- , method: 'GET'
- , preventCache: noCache
- , async: true
- , handleSuccess: callback
- }
- // Get templ text asynchronously, wait for
- // loading to exec the callback
- fleegix.xhr.send(opts);
- } else {
- opts = {
- url: url
- , method: 'GET'
- , preventCache: noCache
- , async: false
- }
- // Get the templ text inline and pass directly to
- // the callback
- text = fleegix.xhr.send(opts);
- callback(text);
- }
- }
- };
-
- this.handleErr = function(e, line) {
- var src = this.templateText.split('\n')
- , ctxt = ''
- , err = new Error()
- , start = Math.max(line - 3, 0)
- , end = Math.min(src.length, line + 3)
- , arr = src.slice(start, end)
- , curr;
-
- for(var i = 0, len = arr.length; i < len; i++) {
- curr = i + start + 1;
- ctxt += (curr == line ? ' >> ' : ' ')
- + curr
- + '| '
- + arr[i] + '\n';
- }
-
- err.name = e.name;
- err.message = 'ejs:' + line + '\n' + ctxt + e.message;
- throw err;
- };
-
- this.process = function(params) {
- var self = this
- , params = params || {}
- , domNode = params.node
- , src
- , fn
- , rethrow;
-
- this.data = params;
- this.source = this.source || ''; // Cache the template source for speed
-
- if(!this.source) this.generateSource();
-
- src = 'var __output = "", __line = 1; with (locals) { try {' +
- this.source + '} catch(e) { rethrow(e, __line); } } return __output;';
- fn = new Function('locals', src);
- rethrow = function(e, line) { self.handleErr(e, line); };
-
- this.markup = fn.call(this, geddy.mixin({
- utils: utils
- , rethrow: rethrow
- }, this.data));
-
- if(domNode) domNode.innerHTML = this.markup;
- return this.markup;
- };
-
- this.generateSource = function() {
- var matches = this.parseTemplateText()
- , line;
-
- if(matches) {
- for(var i in matches) {
- line = matches[i];
-
- if(line) this.scanLine(line);
- }
- }
- };
-
- this.parseTemplateText = function() {
- var str = this.templateText
- , pat = _REGEX
- , result = pat.exec(str)
- , arr = []
- , firstPos
- , lastPos;
-
- while(result) {
- firstPos = result.index;
- lastPos = pat.lastIndex;
-
- if(firstPos !== 0) {
- arr.push(str.substring(0, firstPos));
- str = str.slice(firstPos);
- }
-
- arr.push(result[0]);
- str = str.slice(result[0].length);
- result = pat.exec(str);
- }
-
- if(str !== '') {
- arr.push(str);
- }
-
- return arr;
- };
-
- this.scanLine = function(line) {
- var self = this
- , newLineCount = 0
- , _addOutput;
-
- _addOutput = function() {
- if(self.truncate) line = line.replace('\n', '');
-
- // Preserve literal slashes
- line = line.replace(/\\/g, '\\\\');
-
- // Convert linebreaks
- line = line.replace(/\n/g, '\\n');
- line = line.replace(/\r/g, '\\r');
-
- // Escape double-quotes
- // - this will be the delimiter during execution
- line = line.replace(/"/g, '\\"');
- self.source += '__output += "' + line + '";';
- };
-
- newLineCount = (line.split('\n').length - 1);
-
- switch (line) {
- case '<%':
- this.mode = this.modes.EVAL;
- break;
- case '<%=':
- this.mode = this.modes.ESCAPED;
- break;
- case '<%-':
- this.mode = this.modes.RAW;
- break;
- case '<%#':
- this.mode = this.modes.COMMENT;
- break;
- case '<%%':
- this.mode = this.modes.LITERAL;
- this.source += '__output += "' + line.replace('<%%', '<%') + '";';
- break;
- case '%>':
- case '-%>':
- if(this.mode == this.modes.LITERAL) _addOutput();
-
- this.mode = null;
- this.truncate = line.indexOf('-') == 0;
- break;
- default:
- // In script mode, depends on type of tag
- if (this.mode) {
- switch (this.mode) {
- // Just executing code
- case this.modes.EVAL:
- this.source += line;
- break;
- // Exec, esc, and output
- case this.modes.ESCAPED:
- // Add the exec'd, escaped result to the output
- this.source += '__output += utils.string.escapeXML(' +
- line.replace(/;\S*/, '') + ');';
- break;
- // Exec and output
- case this.modes.RAW:
- // Add the exec'd result to the output
- this.source += '__output += ' + line + ';';
- break;
- case this.modes.COMMENT:
- // Do nothing
- break;
- // Literal <%% mode, append as raw output
- case this.modes.LITERAL:
- _addOutput();
- break;
- }
- }
- // In string mode, just add the output
- else _addOutput();
- }
-
- if(newLineCount) {
- this.currentLine += newLineCount;
- this.source += '__line = ' + this.currentLine + ';';
- }
- };
-};
-
-exports.Template = ejs.Template;
View
165 lib/template/adapters/handlebars.js
@@ -0,0 +1,165 @@
+/*
+ * Geddy JavaScript Web development framework
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+
+var errors = require('../../response/errors')
+ , jade = {};
+
+jade.Template = function(params) {
+ params = params || {};
+
+ // Try to require Jade or error out
+ try {
+ // Note: This isn't actually used here
+ // - mainly it's here so we can use geddy errors to error out with
+ require('handlebars');
+ } catch(err) {
+ new errors.viewError('handlebars');
+ }
+
+ this.mode = null;
+ this.truncate = false;
+ this.templato = params.templato;
+ this.afterLoaded = params.afterLoaded;
+ this.source = '';
+ this.markup = undefined;
+ // Note: If you don't want to use Fleegix.js,
+ // override getTemplateTextFromNode to use
+ // textarea node value for template text
+ this.templateText = params.text || this.getTemplateTextFromNode(params.node);
+
+ // Try to get from URL if no template text
+ if(typeof this.templateText == 'undefined') {
+ // If you don't want to use Fleegix.js,
+ // override getTemplateTextFromUrl to use
+ // files for template text
+ this.getTemplateTextFromUrl(params);
+ }
+};
+
+jade.Template.prototype = new function() {
+
+ this.getTemplateTextFromNode = function(node) {
+ // Requires the fleegix.xhr module
+ if(typeof fleegix.string == 'undefined') throw 'Requires fleegix.string module.';
+ var ret;
+
+ if(node) {
+ ret = node.value;
+ ret = fleegix.string.unescapeXML(ret);
+ ret = fleegix.string.trim(ret);
+ }
+ return ret;
+ };
+
+ this.getTemplateTextFromUrl = function(params) {
+ // Requires the fleegix.xhr module
+ if (typeof fleegix.xhr == 'undefined') throw 'Requires fleegix.xhr module.';
+ var self = this
+ , url = params.url
+ , noCache = params.preventCache || false;
+
+ // If cache is found and caching is on
+ if (text && !noCache) {
+ this.templateText = text;
+ }
+ // Otherwise get the text
+ else {
+ var opts;
+ var callback = function(s) {
+ // Callback for setting templateText and caching
+ // - used for both sync and async loading
+ self.templateText = s;
+ ejs.templateTextCache[url] = s;
+
+ // Use afterLoaded hook if set
+ if (typeof self.afterLoaded == 'function') {
+ self.afterLoaded();
+ }
+ };
+
+ if(params.async) {
+ opts = {
+ url: url
+ , method: 'GET'
+ , preventCache: noCache
+ , async: true
+ , handleSuccess: callback
+ };
+ // Get template text asynchronously, wait for
+ // loading to exec the callback
+ fleegix.xhr.send(opts);
+ }
+ else {
+ opts = {
+ url: url
+ , method: 'GET'
+ , preventCache: noCache
+ , async: false
+ };
+ // Get the template text inline and pass directly to
+ // the callback
+ text = fleegix.xhr.send(opts);
+ callback(text);
+ }
+ }
+ };
+
+ this.process = function(params) {
+ params = params || {}
+ var domNode = params.node
+ , settings;
+
+ this.data = params;
+ this.source = this.source || ''; // Cache generated template for speed
+
+ // If no cache is available generate template
+ //if(!this.source) this.source = this.generateSource();
+ if(!this.source) {
+ settings = {
+ engine: 'handlebars'
+ , template: this.templateText
+ };
+
+ this.templato.set(settings);
+ this.source = this.templato.render(this.data);
+ }
+
+ // Assign the source to the template markup
+ this.markup = this.source;
+
+ if(domNode) domNode.innerHTML = this.markup;
+
+ return this.markup;
+ };
+
+ /*this.generateSource = function() {
+ var str = this.templateText
+ , template
+ , options = {cache: true, filename: this.data.template.file};
+
+ this.engine.render(str, options, function(err, data) {
+ if(err) throw err;
+ template = data;
+ });
+
+ return template;
+ };*/
+
+};
+
+exports.Template = jade.Template;
View
10 lib/template/adapters/jade.js
@@ -121,20 +121,22 @@ jade.Template.prototype = new function() {
this.process = function(params) {
params = params || {}
- var domNode = params.node;
+ var domNode = params.node
+ , settings;
this.data = params;
this.source = this.source || ''; // Cache generated template for speed
// If no cache is available generate template
//if(!this.source) this.source = this.generateSource();
if(!this.source) {
- this.templato.set({
+ settings = {
engine: 'jade'
, template: this.templateText
- });
+ };
- this.source = this.templato.render(this.data);
+ this.templato.set(settings);
+ this.source = this.templato.render(this.data, { hey: 'hey' });
}
// Assign the source to the template markup
View
10 lib/template/adapters/mustache.js
@@ -126,19 +126,21 @@ jade.Template.prototype = new function() {
this.process = function(params) {
params = params || {}
- var domNode = params.node;
+ var domNode = params.node
+ , settings;
this.data = params;
this.source = this.source || ''; // Cache generated template for speed
// If no cache is available generate template
//if(!this.source) this.source = this.generateSource();
if(!this.source) {
- this.templato.set({
- engine: 'jade'
+ settings = {
+ engine: 'mustache'
, template: this.templateText
- });
+ };
+ this.templato.set(settings);
this.source = this.templato.render(this.data);
}

0 comments on commit 422b6f2

Please sign in to comment.