cache template files in memory
files:
- app
- controller
- action
- view
- foo.js
- tpl
- foo.tpl
- bar.tpl
- app.js
app/app.js
var connect = require('connect');
var app = connect();
var path = require('path');
var templateCache = require('template-cache');
var artTemplate = require('art-template');
// cache template files
templateCache.load(path.join(__dirname, './tpl'), {
slim: true,
autocrlf: true,
extension: '.tpl',
engine: artTemplate.compile
});
require('./route)(app);
http.createServer(app).listen(3000);
app/view/foo.js
var template = require('template-cache');
module.exports = function (req, res, next) {
...
var data = {
somekey: 'somevalue'
};
// render the template
var content = template.require('bar')(data);
...
};
In one node process, require('template-cache')
share one same instance. So, if you wanna load two or more template pathes, please try namespace method.
var loggerTemplate = require('template-cache').namespace('logger');
var applicationTemplate = require('template-cache').namespace('app');
// load
loggerTemplate.load(path.join(__dirname, './logger/tpl'), {...});
applicationTemplate.load(path.join(__dirname, './app/tpl'), {...});
// require
console.log(loggerTemplate.require('foo'));
console.log(applicationTemplate.require('bar'));
the load
method can be set with these options.
default: '.tpl'
only the files with this extension will be cached
default: false the program will cache the files in all subpaths when this option is true.
default: true
auto convert the crlf (windows mode) to lf (linux mode)
default: false
cleaning the indents and line feeds in the files.
It's useful for simplifing the content.
tip: You can type \n
when you need a line feed with slim mode.
example.tpl:
<% if (foo) { %>
foo
<% if (bar) { %>
bar\n
<% } %>
<% } %>
<% if (baz) { %>
baz
<% } %>
the value of cache.require('example')
with slim:
<% if (foo) { %>foo<% if (bar) { %>bar\n<% } %><% } %><% if (baz) { %>baz<% } %>
render example.tpl without slim mode:
var cache = require('template-cache');
cache.load('./tpl');
console.log(compile(cache.require('example'))({foo: true, bar: true, baz: true});
output:
foo
bar\n
baz
render example.tpl with slim mode:
var cache = require('template-cache');
cache.load('./tpl', {slim: true});
console.log(compile(cache.require('example'))({foo: true, bar: true, baz: true});
output:
foobar
baz
If you want to get the same result as above one without slim mode, you should write a example.tpl like it:
<%
if (foo) {
%>foo<%
if (bar) {
%>bar\n<%
} %><%
} %><%
if (baz) {
%>baz<%
} %>
no~~~
default: null
engine
should be the compile method of the template render engine like artTemplate, ejs, handlebars and so on.
If you keep this value null, the require
method will return the origin content.
Here is a simple example:
without options.engine:
var cache = require('template-cache');
var artTemplate = require('art-template');
cache.load('./tpl');
then compile and render the template:
return artTemplate.compile(cache.require('example'))({foo: true, bar: true, baz: true});
with options.engine:
var cache = require('template-cache');
var artTemplate = require('art-template');
cache.load('./tpl', {engine: artTemplate.compile});
return cache.require('example'))({foo: true, bar: true, baz: true});
they will return the same result.
The load
method should be called once at least before the app launch.
The require
method will return the origin content without options.engine
, otherwise return the compiled template rendering function.
clear cache box
refresh the cache box
return a json style object which has the key as the filename and the value as the content
namespace
method allow you cache different groups of templates by returning a new cache box
example:
var cache = require('template-cache');
cache.namespace('yelo').load('./tpl');
console.log(cache.namespace('yelo').require('example')({foo: true, bar: true, baz: true}));
var tests = cache.namespace('test');
tests.load('./test/tpl');
console.log(test.require('foobar')({foo: bar}));
the MIT license