squirrelly icon indicating copy to clipboard operation
squirrelly copied to clipboard

[ Feature ] - Preventing null and undefined exception on template variables

Open Javiani opened this issue 3 years ago • 0 comments

Hi All! First of all, thanks for this amazing project, I was looking for something like this to use in my web component library, called Jails, you can find more info here: https://github.com/jails-org/Jails

The Problem

This PR tries to solve a problem that was making it impossible to use in my project, which is the fact that you might not have a variable already set used on iteration and conditions, so on those cases it breaks the entire application instead of returning null or false. I believe that Mustache already have this behavior by default, if you try to use some variable that doesn't exist it just skip it and treat as a falsy / empty value.

The Solution

try/catch is the only solution I've could think to solve that problem, so I wrap all variables with a function that tries to return that variable if it is set and return an empty string otherwise.


I didn't have time to look for all edge cases and I still not familiar with the entire code, and I'm not sure how much it's going to impact on performance, so maybe there's a better way, but I'll be glad if my PR could be usefull somehow even if it can't be merged.

Thanks in advance! Have a good one.

Javiani avatar Aug 13 '22 17:08 Javiani