eta-dev / eta

Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
https://eta.js.org
MIT License
1.41k stars 65 forks source link

Is it possible to remove '=' from left delimiter? #246

Closed sanketdhoble closed 1 year ago

sanketdhoble commented 1 year ago

Query-1: I changed delimter to {{ }} with

eta.configure({
    tags: ['{{', '}}']
})

I still have to print variables using my age is {{= it.age}}. Is it possible to remove this = sign from delimter syntax?

Query-2: Is it possible to remove 'it' for variable and directly print it like this my age is {{age}} instead?

Query-3: eta throws error if age is not present in my nested source object when trying to access, {{it.data.age.test}} Is there a way to overcome this?

nebrelbug commented 1 year ago
  1. Pass in option parse: {exec: "$", interpolate: "", raw: "~"}. Note: to run code, you'll have to prefix it with "$": {{$ if(x) }}
  2. Yes, pass in option useWith: true
  3. Use optional chaining: {{it.data?.age?.test}}
sanketdhoble commented 1 year ago

@nebrelbug Thanks for clarifying point 1 & 2.

Regarding point 3. Optional chaining doesn't work with older node versions ( node 13 and below ). Plus I have to add ? everytime in my template. I was wondering if there is way to do this via eta only similar to other templating tools like liquidJs, twigJs where by default it prints empty string if value is not accessible or undefined.

nebrelbug commented 1 year ago

@sanketdhoble sure, you can define an auto-filter function to do a try-catch around interpolations.

https://eta.js.org/docs/api#auto-filtering-data