Permalink
Browse files

Merge pull request #2 from daraser/master

Support for partials, some documentation and example project
  • Loading branch information...
2 parents 89b07a2 + bad9b7d commit 191b2239ee82bbc1e81525e97b786900a634c332 @cstigler committed Jan 12, 2013
Showing with 164 additions and 4 deletions.
  1. +103 −1 README.md
  2. +24 −0 example_app.js
  3. +29 −2 express-dot.js
  4. +1 −0 index.dot
  5. +5 −0 layout.dot
  6. +1 −1 package.json
  7. +1 −0 partial.dot
View
104 README.md
@@ -1,4 +1,106 @@
express3-dot
============
-doT stub for Express 3.x with caching and layout support
+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.<myParam>}}
+ ...
+ },
+
+ 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 ){ ... }
+});
+...
+```
+
+
+
View
@@ -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);
View
@@ -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);
});
-};
+};
View
@@ -0,0 +1 @@
+Hello world{{#def.load('/partial.dot')}}
View
@@ -0,0 +1,5 @@
+<html>
+ <body>
+ {{=it.body}}
+ </body>
+</html>
View
@@ -10,7 +10,7 @@
"caching",
"engine"
],
- "version": "0.1.0",
+ "version": "0.1.2",
"main": "express-dot.js",
"dependencies": {
"dot": ">= 0.2.6",
View
@@ -0,0 +1 @@
+!

0 comments on commit 191b223

Please sign in to comment.