Added helper method to import a module into the compiled template module

This commit is contained in:
Patrick Steele-Idem 2016-02-03 11:18:50 -07:00
parent 4c419abbe9
commit dfdbf8b377
4 changed files with 27 additions and 0 deletions

View File

@ -130,6 +130,10 @@ class Generator {
this.context.addStaticCode(code);
}
importModule(varName, path) {
return this.context.importModule(varName, path);
}
getStaticVars() {
return this.context.getStaticVars();
}

View File

@ -114,6 +114,15 @@ class CompileContext {
return deresolve(targetFilename, this.dirname);
}
importModule(varName, path) {
if (typeof path !== 'string') {
throw new Error('"path" should be a string');
}
return this.addStaticVar(varName, 'require("' + path + '")');
}
addStaticVar(name, init) {
var actualVarName = this._uniqueVars.addVar(name, init);
this._staticVars[actualVarName] = init;

View File

@ -0,0 +1,13 @@
function create(__helpers) {
var str = __helpers.s,
empty = __helpers.e,
notEmpty = __helpers.ne,
escapeXml = __helpers.x,
foo = require("./foo");
return function render(data, out) {
foo();
};
}
(module.exports = require("marko").c(__filename)).c(create);

View File

@ -0,0 +1 @@
<test-import-module/>