Skip to content

Commit

Permalink
Merge pull request #2 from daraser/master
Browse files Browse the repository at this point in the history
Support for partials, some documentation and example project
  • Loading branch information
cstigler committed Jan 12, 2013
2 parents 89b07a2 + bad9b7d commit 191b223
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 4 deletions.
104 changes: 103 additions & 1 deletion 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 ){ ... }
});
...
```



24 changes: 24 additions & 0 deletions 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);
31 changes: 29 additions & 2 deletions express-dot.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
};

Expand All @@ -61,4 +88,4 @@ exports.__express = function(filename, options, cb) {

return _renderWithLayout(filename, layoutTemplate, options, cb);
});
};
};
1 change: 1 addition & 0 deletions index.dot
@@ -0,0 +1 @@
Hello world{{#def.load('/partial.dot')}}
5 changes: 5 additions & 0 deletions layout.dot
@@ -0,0 +1,5 @@
<html>
<body>
{{=it.body}}
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -10,7 +10,7 @@
"caching",
"engine"
],
"version": "0.1.0",
"version": "0.1.2",
"main": "express-dot.js",
"dependencies": {
"dot": ">= 0.2.6",
Expand Down
1 change: 1 addition & 0 deletions partial.dot
@@ -0,0 +1 @@
!

0 comments on commit 191b223

Please sign in to comment.