memory.js
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var path = require('path'),
utils = require('../utils');
/**
* Loads templates from a provided object mapping.
* @alias swig.loaders.memory
* @example
* var templates = {
* "layout": "{% block content %}{% endblock %}",
* "home.html": "{% extends 'layout.html' %}{% block content %}...{% endblock %}"
* };
* swig.setDefaults({ loader: swig.loaders.memory(templates) });
*
* @param {object} mapping Hash object with template paths as keys and template sources as values.
* @param {string} [basepath] Path to the templates as string. Assigning this value allows you to use semi-absolute paths to templates instead of relative paths.
*/
module.exports = function (mapping, basepath) {
var ret = {};
basepath = (basepath) ? path.normalize(basepath) : null;
/**
* Resolves <var>to</var> to an absolute path or unique identifier. This is used for building correct, normalized, and absolute paths to a given template.
* @alias resolve
* @param {string} to Non-absolute identifier or pathname to a file.
* @param {string} [from] If given, should attempt to find the <var>to</var> path in relation to this given, known path.
* @return {string}
*/
ret.resolve = function (to, from) {
if (basepath) {
from = basepath;
} else {
from = (from) ? path.dirname(from) : '/';
}
return path.resolve(from, to);
};
/**
* Loads a single template. Given a unique <var>identifier</var> found by the <var>resolve</var> method this should return the given template.
* @alias load
* @param {string} identifier Unique identifier of a template (possibly an absolute path).
* @param {function} [cb] Asynchronous callback function. If not provided, this method should run synchronously.
* @return {string} Template source string.
*/
ret.load = function (pathname, cb) {
var src, paths;
paths = [pathname, pathname.replace(/^(\/|\\)/, '')];
src = mapping[paths[0]] || mapping[paths[1]];
if (!src) {
utils.throwError('Unable to find template "' + pathname + '".');
}
if (cb) {
cb(null, src);
return;
}
return src;
};
return ret;
};