0

我只是扩展了文件系统加载器,例如:

var nunjucks = require('nunjucks');

MyLoader = nunjucks.FileSystemLoader.extend({

        getSource: function(name) {

            return this.parent().getSource(name);
        }

 });

我怎么总是得到:

TypeError: Path must be a string. Received undefined
    at assertPath (path.js:8:11)
    at Object.posix.resolve (path.js:426:5)
    at Loader.extend.getSource (/home/myname/myapp/node_modules/nunjucks/src/node-loaders.js:60:26

如何getSource正确调用父方法?

请帮忙。谢谢

4

1 回答 1

0
var CustomLoader = nunjucks.FileSystemLoader.extend({
    getSource: function(name) {
        var result = nunjucks.FileSystemLoader.prototype.getSource.call(this, name); // !!!

        if (!result) 
            return null;

        result.src = ... // do-smth-with-content
        return result;
    }
}); 

var env = new nunjucks.Environment(
    new CustomLoader([your-template-paths], { watch: false, noCache: false }), 
    {autoescape: true});
env.express(app); // don't forget connect to express

您可以在github (nunjucks-minify-loaders)上找到更多详细信息

于 2016-08-29T16:56:44.970 回答