eta-dev / eta

Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript

Home Page:https://eta.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add E variable in v3 as it was in v2

petercoppensdatylon opened this issue · comments

Hello,

I had a template using V2, that sort of had this

const etaConfig = {
  async: true,
  autoEscape: false,
  views: __dirname + '/../reports',
  functions: {
    require: require
  },
};

....
const eta = new Eta(etaConfig);
....
eta.render(template, {})

And then I could use the following in the template

<%
mod = E.functions.require("....");
...

That no longer seems to work. I get an exception

ReferenceError: E is not defined

Is there some other way to access the configuration in the templates?

Tx!

Peter

@petercoppensdatylon I love the creativity of this usage! I'd recommend something like the following.

First, extend the Eta class:

const functions = {
	require: require
}

export class NewEta extends Eta {
  functions = functions;
}

Then create a new instance using functionHeader:

const eta = new NewEta({ functionHeader: "const functions = this.functions" })

Inside your template, you'll then be able to write functions.require("...").

Let me know how this works for you!

Hello,

Thanks for this tip. Really appreciate it. I first worked around it by passing in the functions through the render parameters, but I changed it using your suggestion. I like it better.

Best,

Peter