twig.exports.js | |
---|---|
| |
twig.function.jsThis file provides extension points and other hooks into the twig functionality. | var Twig = (function (Twig) {
"use strict";
Twig.exports = {};
/**
* Create and compile a twig.js template.
*
* @param {Object} param Paramteres for creating a Twig template.
*
* @return {Twig.Template} A Twig template ready for rendering.
*/
Twig.exports.twig = function twig(params) {
'use strict';
var id = params.id,
options = {
strict_variables: params.strict_variables || false
};
if (id) {
Twig.validateId(id);
}
if (params.debug !== undefined) {
Twig.debug = params.debug;
}
if (params.trace !== undefined) {
Twig.trace = params.trace;
}
if (params.data !== undefined) {
return new Twig.Template({
data: params.data,
module: params.module,
id: id,
options: options
});
} else if (params.ref !== undefined) {
if (params.id !== undefined) {
throw new Error("Both ref and id cannot be set on a twig.js template.");
}
return Twig.Templates.load(params.ref);
} else if (params.href !== undefined) {
return Twig.Templates.loadRemote(params.href, {
id: id,
module: params.module,
precompiled: params.precompiled,
method: 'ajax',
async: params.async,
options: options
}, params.load, params.error);
} else if (params.path !== undefined) {
return Twig.Templates.loadRemote(params.path, {
id: id,
module: params.module,
precompiled: params.precompiled,
method: 'fs',
async: params.async,
options: options
}, params.load, params.error);
}
}; |
Extend Twig with a new filter. | Twig.exports.extendFilter = function(filter, definition) {
Twig.filter.extend(filter, definition);
};
|
Extend Twig with a new function. | Twig.exports.extendFunction = function(fn, definition) {
Twig._function.extend(fn, definition);
}; |
Extend Twig with a new test. | Twig.exports.extendTest = function(test, definition) {
Twig.test.extend(test, definition);
}; |
Extend Twig with a new definition. | Twig.exports.extendTag = function(definition) {
Twig.logic.extend(definition);
};
/**
* Provide an extension for use with express 2.
*
* @param {string} markup The template markup.
* @param {array} options The express options.
*
* @return {string} The rendered template.
*/
Twig.exports.compile = function(markup, options) {
var id = options.filename,
sep_chr = '/',
path = options.filename,
template;
|
Try to load the template from the cache | template = new Twig.Template({
data: markup,
path: path,
id: id,
options: options.settings['twig options']
}); // Twig.Templates.load(id) ||
return function(context) {
return template.render(context);
};
};
/**
* Provide an extension for use with express 3.
*
* @param {string} path The location of the template file on disk.
* @param {Object|Function} The options or callback.
* @param {Function} fn callback.
*/
Twig.exports.renderFile = function(path, options, fn) { |
handle callback in options | if ('function' == typeof options) {
fn = options, options = {};
}
var view_options = options.settings['twig options'],
option,
params = {
path: path,
load: function(template) { |
render and return template | fn(null, template.render(options));
}
};
|
mixin any options provided to the express app. | if (view_options) {
for (option in view_options) {
params[option] = view_options[option];
}
}
Twig.exports.twig(params);
};
Twig.exports.__express = Twig.exports.renderFile;
/**
* Shoud Twig.js cache templates.
* Disable during development to see changes to templates without
* reloading, and disable in production to improve performance.
*
* @param {boolean} cache
*/
Twig.exports.cache = function(cache) {
Twig.cache = cache;
}
return Twig;
}) (Twig || { });
|