premasagar / tim

A tiny, secure JavaScript micro-templating script.
251 stars 26 forks source link

Respond to null values #4

Open premasagar opened 13 years ago

premasagar commented 13 years ago

Currently, tim throws an exception that {{property}} not closed when it encounters a null value. This is because the typeof null === "object" and it is assuming this is an object.

Either nulls should be ignored, or the error message should be more clear.

symroe commented 11 years ago

Hitting this with an API that I can't change....

Bit of a deal breaker for using tim for me. :(

Can you suggest a work around?

premasagar commented 11 years ago

Not tested, but it should be possible to add another else if at line 226:

else if (substituted === null){
    template = '';
}

This will return an empty string, instead of throwing an error. Pull requests welcome ;)

symroe commented 11 years ago

Not tested in tinytim (I guess this bug still applies?), and not minified (not sure what method you used for minifying).

premasagar commented 11 years ago

Great. Thanks @symroe.

I'm thinking that tinytim's block at line 47 should be changed to:

if (lookup === undef || lookup === null){
    return '';
}

So that it no longer throws an error if the property was not found. Throwing an error can be useful, for example, to catch problems in one's data-manipulation logic that may not be easily seen, but it is probably more hassle than it's worth.