underscore: _.template: allow undefined keys
When the template function needs an element that is not defined it returns an Error. It would be great if the template function would act equivalent as if the value of the element is undefined
_.template('<%= foo %>', {foo: undefined});
returns “”, but
_.template('<%= foo %>', {});
returns a ReferenceError
The reason I’m requesting this is, that I usually use underscore to template my views. But sometimes the attributes of the model are not yet available. In this case the application returns an error, it would be better if the template function would be more lax.
About this issue
- Original URL
- State: closed
- Created 13 years ago
- Comments: 29 (10 by maintainers)
This bug really sucks. I’m not going to use underscore templates again. -.-
All of these are unnecessary. If you really want to not have to define the variables you’re going to use, just use a data object with properties.
Instead of:
Do:
And then in your code, you can refer to
data.a,data.b, ordata.zwithout getting a reference error.It’s an old subject, but you always can access all variables with arguments[0]
<%= arguments[0].foo %> works if foo is undefined
Minifiers should not break that as it’s a static string, but can we reasonably rely on that? It’s arguaby more elegant than
arguments[0].Edit: documentation says
So I’d say it’s even more “public API” than
arguments[0]and you should definitely prefer this 😃@velikayikci
Please note that this only compiles the template. It doesn’t render it. Unless you’re using a very old version of Underscore.
To anyone still running into issues like these, two words of advice.
Firstly, explicitly setting the
variablewhen compiling the template will let you access arbitrary unset properties on the root data object unpunished and also give you better performance.I like naming this variable
root. Another common choice isdata. I also liked a suggestion earlier in this issue thread to useo.Secondly, Underscore nowadays has
hasandget. Using these in your templates will let you access arbitrarily nested properties in the data, no nestedifstatements required.Why doesn’t
_.undefined(foo)even work within templates? This is absurd