diff --git a/README.md b/README.md index 563254b..1ce5090 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,106 @@ express3-dot ============ -doT stub for Express 3.x with caching and layout support \ No newline at end of file +doT stub for Express 3.x with caching and layout support. This an edited version with patial loading support. + +## Install + +Install original repository + +``` +$ npm install express-dot +``` + +or you can use my fork on github + +``` +$ npm install https://www.github.com/daraser/express-dot/tarball/master +``` +Warnning not tested yet + +##Setup + +``` +... + +// load express doT +var doT = require('express-dot'); + +// (optional) set globals any thing you want to be exposed by this in {{= }} and in def {{# }} +doT.setGlobals({ ... }); + +// setup rendering + +app.configure(function() { + + ... + + // set views folder + app.set('views', __dirname + '/views'); + + // doT engine + app.set('view engine', 'dot' ); + app.engine('dot', doT.__express ); + + ... + +}); + +app.get('/', function(req, res){ + // you need to have in views folder + res.render('index', { }); +}) + +``` + +## Options + +You can set up parts of rendering + +``` +... +res.render( + '...', // current body template will be passed to layout template as {{=it.body}} + + { + // cache should be set to true in production enviroment. + cache : false, + // null - default behavior (will look for [viewDir]/layout.dot file; + // boolean - makes doT render without layout file, + // string path - looks for *.dot file to use for layout + layout : false, + // any other data which you want to be exposed for the template by {{=it.}} + ... + }, + + function(err, str_template){ + // callback + } +); +... +``` +## Globals + +Globals are exposed as {{#def}} and {{= this}}. So anything you want to use globaly should be exposed. + +``` +... +doT.setGlobals({ + + // set any function or property to be exposed in template + ..., + + // global configuration + // default is false, set true in production enviroment to cache partials + partialCache : false, + + // reserved functionality will throw error if globals have + // load is reserved for partial loading. {{#def.load('/patials/sample.dot')}} it will load partial template from + // __dirname + file path + load : function(path ){ ... } +}); +... +``` + + + diff --git a/example_app.js b/example_app.js new file mode 100644 index 0000000..5f16d4c --- /dev/null +++ b/example_app.js @@ -0,0 +1,24 @@ +var express = require('express'); +var app = express(); + +// load express doT +var doT = require('express-dot'); + +app.configure(function() { + + // set views folder + app.set('views', __dirname); + + // doT engine + app.set('view engine', 'dot' ); + app.engine('dot', doT.__express ); + +}); + +// respond +app.get('/', function(req, res, next){ + // you need to have in views folder + res.render('index', { }); +}); + +app.listen(3000); diff --git a/express-dot.js b/express-dot.js index 19474f1..8a28add 100644 --- a/express-dot.js +++ b/express-dot.js @@ -2,9 +2,29 @@ var fs = require('fs'); var path = require('path'); var doT = require('dot'); var async = require('async'); +var path = require('path'); var _cache = {}; -var _globals = {}; +var _partialsCache = {}; +var _globals = { + load : function(file) { + var template = null; + // let's try loading content from cache + if(_globals.partialCache == true) + template = _partialsCache[file]; + + // no content so let's load from file system + if(template == null){ + template = fs.readFileSync(path.join(path.dirname(process.argv[1]), file)); + } + + // let's cache the partial + if(_globals.partialCache == true) + _partialsCache[file] = template; + + return template; + } +}; function _renderFile(filename, options, cb) { 'use strict'; @@ -37,6 +57,13 @@ function _renderWithLayout(filename, layoutTemplate, options, cb) { exports.setGlobals = function(globals) { 'use strict'; + for(var f in _globals){ + if(globals[f] == null){ + globals[f] = _globals[f]; + } + else + throw new Error("Your global uses reserved utility: " + f); + } _globals = globals; }; @@ -61,4 +88,4 @@ exports.__express = function(filename, options, cb) { return _renderWithLayout(filename, layoutTemplate, options, cb); }); -}; \ No newline at end of file +}; diff --git a/index.dot b/index.dot new file mode 100644 index 0000000..09b7210 --- /dev/null +++ b/index.dot @@ -0,0 +1 @@ +Hello world{{#def.load('/partial.dot')}} diff --git a/layout.dot b/layout.dot new file mode 100644 index 0000000..668f0f7 --- /dev/null +++ b/layout.dot @@ -0,0 +1,5 @@ + + + {{=it.body}} + + diff --git a/package.json b/package.json index 12b829b..4f794d7 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "caching", "engine" ], - "version": "0.1.0", + "version": "0.1.2", "main": "express-dot.js", "dependencies": { "dot": ">= 0.2.6", diff --git a/partial.dot b/partial.dot new file mode 100644 index 0000000..cdf4cb4 --- /dev/null +++ b/partial.dot @@ -0,0 +1 @@ +!