eta icon indicating copy to clipboard operation
eta copied to clipboard

Ability to provide a custom function to fetch templates given their path

Open ewired opened this issue 3 years ago • 2 comments

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.

ewired avatar Mar 19 '21 19:03 ewired

This should be relatively simple, just create an fs attr on the config to hold the methods that are needed.

shadowtime2000 avatar Mar 23 '21 23:03 shadowtime2000

@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")
});

issuefiler avatar May 17 '21 15:05 issuefiler

I think this is a fantastic idea and would like to implement it in Eta version 2.

nebrelbug avatar Jan 01 '23 01:01 nebrelbug

Implemented in Eta v3!

nebrelbug avatar Jun 13 '23 20:06 nebrelbug