Marko v3: Added methods for resolving templates and paths

This commit is contained in:
Patrick Steele-Idem 2016-02-09 15:21:25 -07:00
parent 7fd574deb4
commit 87ed39139b
2 changed files with 36 additions and 0 deletions

View File

@ -617,6 +617,14 @@ class Generator {
onDone(listenerFunc) {
this._doneListeners.push(listenerFunc);
}
getRequirePath(targetFilename) {
return this.context.getRequirePath(targetFilename);
}
resolvePath(pathExpression) {
return this.context.resolvePath(pathExpression);
}
}
module.exports = Generator;

View File

@ -375,6 +375,34 @@ class CompileContext {
createWalker(options) {
return new Walker(options);
}
/**
* Statically resolves a path if it is a literal string. Otherwise, it returns the input expression.
*/
resolvePath(pathExpression) {
ok(pathExpression, '"pathExpression" is required');
if (pathExpression.type === 'Literal') {
let path = pathExpression.value;
if (typeof path === 'string') {
return this.addStaticVar(path, this.builder.requireResolve(pathExpression));
}
}
return pathExpression;
}
resolveTemplate(pathExpression) {
ok(pathExpression, '"pathExpression" is required');
if (pathExpression.type === 'Literal') {
let path = pathExpression.value;
if (typeof path === 'string') {
return this.importTemplate(path);
}
}
return pathExpression;
}
}
module.exports = CompileContext;