Skip to content

Commit

Permalink
updates to cjsTemplate to expose a few different methods to consume i…
Browse files Browse the repository at this point in the history
…t. Use fs spec for reading files.
  • Loading branch information
Dustin Machi committed Jun 10, 2010
1 parent d44135b commit 284f168
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
33 changes: 24 additions & 9 deletions lib/cjsTemplate.js
@@ -1,4 +1,4 @@
var ZParse = require('./zparse').ZParse;
var ZParse = require('./zparse').ZParse

var Implementation = {
statement: {
Expand Down Expand Up @@ -67,7 +67,7 @@ var Implementation = {
type: 'single',
handler: function(tree, content, caller){
var templateName = tree.arguments.template;
var templateString = getTemplate(templateName);
var templateString = require("./templateResolver").get(templateName);
//print("Do IMPORT of template: " + templateName + ":" + templateString);
var subparser = new ZParse(caller.implementation || Implementation);
var fullFunc = subparser.parse(templateString);
Expand All @@ -81,7 +81,7 @@ var Implementation = {
handler: function(tree, content, caller){
var templateName = tree.arguments.template;
var rootContext = tree.arguments.rootContext;
var templateString = getTemplate(templateName);
var templateString = require("./templateResolver").get(templateName);
//print("Do RENDER of template: " + templateName + ": " + templateString);
var subparser = new ZParse(caller.implementation || Implementation);
var fullFunc = subparser.parse(templateString);
Expand Down Expand Up @@ -112,7 +112,7 @@ var Implementation = {
//'print("serialized body item: " + serialize(', element, '));',
//'var ', element, '=el;',content,
content,
'});',
'});'
].join('');
}
},
Expand Down Expand Up @@ -245,8 +245,23 @@ var Implementation = {
}
};

exports.Template= function(templateString){
var x = new ZParse(Implementation)
x.parse(templateString);
return x;
}
exports.Template = function(custom){
new ZParse(custom || Implementation);
};

exports.renderTemplate = function(templateString, context){
var template = new ZParse(Implementation);
template.parse(templateString);
return template.process(context);
};

exports.getCompiledTemplate = function(templateString){
var template = new ZParse(Implementation);
template.parse(templateString);
return template;
};

exports.getTemplateFunction = function(templateString){
var template = new ZParse(Implementation);
return template.parse(templateString);
};
42 changes: 42 additions & 0 deletions lib/templateResolver.js
@@ -0,0 +1,42 @@
var fs = require("file");
var templateDir = (require("commonjs-utils/settings").templateFolder || "templates/");
var templateExtension = (require("commonjs-utils/settings").templateExtension || ".html");

exports.get = function(name){
var nameParts = name.split("/");
var templateName = templateDir + name;


if (fs.isDirectory(templateName) && !fs.isFile(templateName+templateExtension)){
templateName = templateName + "/" + nameParts[nameParts.length-1] + templateExtension;
}else{
templateName = templateName + templateExtension;
}

if (fs.isFile(templateName)){
return fs.read(templateName);
}else{
var ending = nameParts.pop();
templateName = templateDir + nameParts.join("/");
if (fs.isDirectory(templateName)) {
templateName = templateName +"/"+ nameParts[nameParts.length-1] + templateExtension;
if (fs.isFile(templateName)){
return readFile(templateName);
}
}else{
//twice we popped one off above
nameParts.push(ending);
nameParts.push(ending);
}
}

var found=false;
while(nameParts.length>0){
var templateName = templateDir + nameParts.join("/") + templateExtension;
if (fs.isFile(templateName)){
return fs.read(templateName);
}
nameParts.pop();
}
};

2 changes: 1 addition & 1 deletion lib/zparse.js
Expand Up @@ -121,7 +121,7 @@ ZParse.prototype.parse = function(source) {
ZParse.prototype.extendTemplate=function(extension){
//print("Extend Template")
//print("\t" + serialize(extension));
var templateString = getTemplate(extension.templateName)
var templateString = require("./templateResolver").get(extension.templateName)
//print("Got Template String. Length: " + templateString.length);
var extendedTemplate = new ZParse(this.implementation, extension);
//print("Begin Parse of Extended Template");
Expand Down

0 comments on commit 284f168

Please sign in to comment.