Skip to content

Commit

Permalink
Provisional support for Express 2.
Browse files Browse the repository at this point in the history
Add support for Express 2.0 `compile()` method.
This makes jqtpl compatible with Express 2.0.
Backwards compatible with prior versions of
Express, by virtue of leaving the existing
`render()` method untouched.

There are no new tests included, and I haven't
verified the old tests still pass, as I can't
get qunit to run on Node 0.4.2... :(
  • Loading branch information
laurie71 committed Mar 7, 2011
1 parent 3a5d952 commit 1020655
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
39 changes: 38 additions & 1 deletion lib/jqtpl.js
@@ -1,3 +1,5 @@
exports.x = __filename;

/*jslint evil: true*/
/**
* Port of jQuery's Template Engine to Nodejs.
Expand Down Expand Up @@ -107,7 +109,7 @@ exports.tag = {


/**
* Support Express render method
* Support Express render method (used by Express < 2.0)
*
* @param {string} markup html string.
* @param {Object} options
Expand Down Expand Up @@ -140,6 +142,41 @@ exports.render = function(markup, options) {
return tpl;
};

/**
* Support Express compile method (used by Express >= 2.0)
*
* @param {string} markup html string.
* @param {Object} options
* `locals` Local variables object.
* `cache` Compiled functions are cached, requires `filename`.
* `filename` Used by `cache` to key caches.
* `scope` Function execution context.
* `debug` Output generated function body.
*
* @return {string} rendered html string.
* @export
*/
exports.compile = function(markup, options) {
var name = options.filename || markup;

// precompile the template and cache it using filename
exports.template(name, markup);

return function render(options) {
var tpl = exports.tmpl(name, options);

if (options.debug) {
exports.debug(exports.template[name]);
}

if (!options.cache) {
delete exports.template[name];
}

return tpl;
}
};

/**
* Unescape template.
* @param {string} args template.
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "jqtpl",
"description": "A port of jQuery's template engine",
"version": "0.0.91",
"version": "0.0.100",
"author": "Oleg Slobodskoi <oleg008@gmail.com>",
"contributors": [
{ "name": "John Resig", "email": "jeresig@gmail.com" },
Expand All @@ -16,7 +16,7 @@
"main": "./lib/jqtpl.js",
"engines": { "node": ">= 0.3.7" },
"dependencies": {
"express": ">=1.0.7 <2.0"
"express": ">=1.0.7 || ~2.0"
},
"devDependencies": {
"qunit": ">= 0.1.3"
Expand Down

0 comments on commit 1020655

Please sign in to comment.