-
Notifications
You must be signed in to change notification settings - Fork 650
Adding file locations to templates #1482
Copy link
Copy link
Open
Description
I'm not quite sure where to put this, but previously in EJS I had a fileLoader hook for adding "preambles" and "postambles" to file renderings. Does nunjucks have an equivalent?
EJS:
import ejs from "ejs"
let ejsFileLoader = function (filePath: string) {
const preamble = `<!-- template_start: ${path.relative(getRootDir(), filePath)} -->`
const postamble = `<!-- template_end: ${path.relative(getRootDir(), filePath)} -->`
return preamble + fs.readFileSync(filePath) + postamble;
};
ejs.fileLoader = ejsFileLoaderThe best I came up with for Nunjucks is the following:
const render = nunjucks.render
nunjucks.render = function (file: string, ...args: any[]) {
const preamble = `<!-- template_start: ${path.relative(getRootDir(), file)} -->`
const postamble = `<!-- template_end: ${path.relative(getRootDir(), file)} -->`
return preamble + render.call(nunjucks, file, ...args) + postamble
}but that feels wrong. Am I missing something obvious using the FileLoader or Environment APIs?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels