squirrellyjs / squirrelly

Semi-embedded JS template engine that supports helpers, filters, partials, and template inheritance. 4KB minzipped, written in TypeScript ⛺
https://squirrelly.js.org
MIT License
555 stars 81 forks source link

Question: Is there a way remove the requirement to prefix 'it'? #218

Closed brsanthu closed 2 years ago

brsanthu commented 3 years ago

Thank you for the library.

Couple of questions.

  1. Is it possible to refer to input variables without having to prefix them with 'it' in templates?
  2. Is it possible to default the value to empty if variable is not defined? For ex., if we refer to {{user.firstName}} and if user is not defined, can we assume it is empty instead of compile error?
jasonfutch commented 3 years ago

The 'it' is a nice feature but shouldn't be required as it is one of the reasons keeping me upgrading to version 8. I also feel that 'it' property name should be definable by the developer.

jasonfutch commented 3 years ago

After reviewing the code looks like you can pass varName option to rename the variable to something else besides 'it'.

https://github.com/squirrellyjs/squirrelly/blob/572a643/src/config.ts#L50

Shanery commented 3 years ago

From Someone else:

If you actually want to be able to write something like: {{ reference }} rather than {{ it.reference }}, simply render as such:

const myTemplate = 'My favorite template engine is {{reference}}.'
const data = {
  reference: 'Squirrelly'
}
const result = Sqrl.render(myTemplate, data, { useWith: true })
brsanthu commented 2 years ago

Looks like that requirement of prefixing with 'it' is here to stay so will close this issue.