eta
eta copied to clipboard
Ability to provide a custom function to fetch templates given their path
Is your feature request related to a problem? Please describe.
In Deno, I want Eta to use Leaf.readTextFileSync
instead of Deno.readTextFileSync
for reading templates referenced by path. Leaf is a library for Deno which creates a kind of virtual filesystem in which files like Eta templates can be bundled and therefore included in compiled executables.
Describe the solution you'd like
To provide a custom function in config to read text files: { readFile: (path: string) => Promise<string> | string }
. This would allow (path) => Leaf.readTextFileSync(path)
, but could also be useful with (path) => fetch(path).then(r => r.text())
.
Describe alternatives you've considered
Forego using paths and use Eta.templates.define()
on every template in advance, then only reference templates by their defined names.
This should be relatively simple, just create an fs
attr on the config to hold the methods that are needed.
@shadowtime2000
How? eta.es.js
filters values to configure.
// eta.es.js
function hasOwnProp(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function copyProps(toObj, fromObj) {
for (var key in fromObj) {
if (hasOwnProp(fromObj, key)) {
toObj[key] = fromObj[key];
}
}
return toObj;
}
/** Update Eta's base config */
function configure(options) {
return copyProps(config, options);
}
// I can’t use `E.indent` when I `import` Eta instead of `require`-ing it.
Eta.configure({
autoTrim: false,
indent: (code, depth, space = "\t") => code
.split("\n")
.map(line => (line ? (space.repeat(depth) + line) : line))
.join("\n")
});
I think this is a fantastic idea and would like to implement it in Eta version 2.
Implemented in Eta v3!