Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updates to cjsTemplate to expose a few different methods to consume i…

…t. Use fs spec for reading files.
  • Loading branch information...
commit 284f168471e8e8ee9203058a0aef330543218a3f 1 parent d44135b
@dmachi authored
Showing with 67 additions and 10 deletions.
  1. +24 −9 lib/cjsTemplate.js
  2. +42 −0 lib/templateResolver.js
  3. +1 −1  lib/zparse.js
View
33 lib/cjsTemplate.js
@@ -1,4 +1,4 @@
-var ZParse = require('./zparse').ZParse;
+var ZParse = require('./zparse').ZParse
var Implementation = {
statement: {
@@ -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);
@@ -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);
@@ -112,7 +112,7 @@ var Implementation = {
//'print("serialized body item: " + serialize(', element, '));',
//'var ', element, '=el;',content,
content,
- '});',
+ '});'
].join('');
}
},
@@ -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);
+};
View
42 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();
+ }
+};
+
View
2  lib/zparse.js
@@ -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");
Please sign in to comment.
Something went wrong with that request. Please try again.