hexo/node_modules/swig-templates/lib/loaders/index.js

54 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-10-03 11:14:36 +08:00
/**
* @namespace TemplateLoader
* @description Swig is able to accept custom template loaders written by you, so that your templates can come from your favorite storage medium without needing to be part of the core library.
* A template loader consists of two methods: <var>resolve</var> and <var>load</var>. Each method is used internally by Swig to find and load the source of the template before attempting to parse and compile it.
* @example
* // A theoretical memcached loader
* var path = require('path'),
* Memcached = require('memcached');
* function memcachedLoader(locations, options) {
* var memcached = new Memcached(locations, options);
* return {
* resolve: function (to, from) {
* return path.resolve(from, to);
* },
* load: function (identifier, cb) {
* memcached.get(identifier, function (err, data) {
* // if (!data) { load from filesystem; }
* cb(err, data);
* });
* }
* };
* };
* // Tell swig about the loader:
* swig.setDefaults({ loader: memcachedLoader(['192.168.0.2']) });
*/
/**
* @function
* @name resolve
* @memberof TemplateLoader
* @description
* 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.
* @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}
*/
/**
* @function
* @name load
* @memberof TemplateLoader
* @description
* Loads a single template. Given a unique <var>identifier</var> found by the <var>resolve</var> method this should return the given template.
* @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.
*/
/**
* @private
*/
exports.fs = require('./filesystem')
exports.memory = require('./memory')