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. Use global functions in interpolation or js mode #257

Open jaimegarza opened 11 months ago

jaimegarza commented 11 months ago

I have the need to declare global functions that I want to make available in the curly braces, like this

function md5(str) {
    return "as23sfgo94kl9sde445asdfloremipsumdolor"; // fake of course
}

Then {{md5(it.description)} or {{! md5(it.description)}

I get told that md5 is not defined. This is just a sample of what I am trying to do. Any guidance of how to make globals available, including functions? I know that I can do it as a filter, but I have the need for other things, like {{weekadd(it.someDate, 2)}} etc

nebrelbug commented 11 months ago

I'd probably recommend using filters, defining a helper, or passing the function in through it.