Skip to content

Commit

Permalink
writing out templates to the filesystem works
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronblohowiak committed May 21, 2011
1 parent 12a08cc commit d8d8676
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/api/_toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* [Templates](views.html#templates)
* [LiveRenders](views.html#liveRender)
* [Persistence](persistence.html)
* [Magic Values](magic_values.html)
* [Options](options.html)

### Transitive Links

Expand Down
4 changes: 3 additions & 1 deletion doc/api/all.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
---
@include views.md
---
@include magic_values
@include persistence
---
@include options
Empty file removed doc/api/magic_values.md
Empty file.
9 changes: 9 additions & 0 deletions doc/api/options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### directories

* templates
* generated

### writeTemplates

boolean - should the compile step write out a template to `options.generated + "/templates.js"` ?

3 changes: 2 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
directories:{
templates:"templates"
templates:"templates",
generated:"generated"
},
templateEngines:{
"haml": function(source){
Expand Down
13 changes: 12 additions & 1 deletion lib/views/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var fs = require("fs"),

function compile(options){
var viewDir = (options.root + "/" + options.directories.templates);
options.writeTemplates = ("writeTemplates" in options ? options.writeTemplates : true);

var config = {};

config.templateFileNames = compile.filenamesForPaths(viewDir);
Expand All @@ -20,9 +22,18 @@ function compile(options){
return filename.substr(viewDir.length + 1);
};

return sharedViews.filenamesToTemplates(config).functions;
var templateFunctions = sharedViews.filenamesToTemplates(config).functions;

if(options.writeTemplates){
compile.write(templateFunctions, options.directories.generated);
}

return templateFunctions;
}

compile.write = function(functions, dir){
sharedViews.writeTemplateStrings(dir+ "/templates.js", "module.exports", functions);
};

compile.filenamesForPaths = function(paths){
var filenames = [];
Expand Down
15 changes: 15 additions & 0 deletions lib/views/render_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,20 @@ module.exports = RenderContext = function (templates){
return this[templateName](locals);
};

// context.renderLive = function(tetherName, templateName, obj){
// var data = this.data;
//
// data.subscribe.push(obj.id);
// data.objectsReferenced[obj.id] = obj;
//
// var binding = ViewBinding(templateName, obj.id, tetherName);
// data.bindings.push(binding);
//
// var output = '<div id="'+binding.elmId+'" class="'+tetherName+'">';
// output = output + this.live[tetherName].call(this, templateName, obj);
// output = output + "</div>";
// return output;
// };

return context;
};
27 changes: 27 additions & 0 deletions test/test_generated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var x = new (require("transitive"))();
var fs = require("fs");
var folder = process.cwd()+"/test_temp/"+require("uuid-pure").newId();
var options = {};

exports.setUp = function(test, assert){
process.chdir("./test/views");
fs.mkdirSync(folder, "777");

options = x.loadOptions({});

options.directories.generated = folder;
options.mergeDefault = false;
x.options = options;

test.finish();
};

exports["test generated templates"] = function (test, assert) {

var functions = x.Views.compile(options);

templates = require(folder+"/templates.js");

assert.equal(templates["user/profile"]({name:"aaron"}), "<h1>AARON</h1>");
test.finish();
};
9 changes: 7 additions & 2 deletions test/test_views_compile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
var x = new (require("transitive"))();
var x = new (require("transitive"))(),
fs = require("fs");

x.options = {};

exports.setUp = function(test, assert){
process.chdir("./test/views");
x.boot(this);

x.options = x.loadOptions({});
x.options.writeTemplates = false;

test.finish();
};

Expand Down

0 comments on commit d8d8676

Please sign in to comment.