{"action":"created","issue":{"url":"https://api.github.com/repos/jashkenas/underscore/issues/237","labels_url":"https://api.github.com/repos/jashkenas/underscore/issues/237/labels{/name}","comments_url":"https://api.github.com/repos/jashkenas/underscore/issues/237/comments","events_url":"https://api.github.com/repos/jashkenas/underscore/issues/237/events","html_url":"https://github.com/jashkenas/underscore/issues/237","id":1156034,"number":237,"title":"_.template: allow undefined keys","user":{"login":"mren","id":576360,"avatar_url":"https://avatars.githubusercontent.com/u/576360?v=3","gravatar_id":"","url":"https://api.github.com/users/mren","html_url":"https://github.com/mren","followers_url":"https://api.github.com/users/mren/followers","following_url":"https://api.github.com/users/mren/following{/other_user}","gists_url":"https://api.github.com/users/mren/gists{/gist_id}","starred_url":"https://api.github.com/users/mren/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mren/subscriptions","organizations_url":"https://api.github.com/users/mren/orgs","repos_url":"https://api.github.com/users/mren/repos","events_url":"https://api.github.com/users/mren/events{/privacy}","received_events_url":"https://api.github.com/users/mren/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/jashkenas/underscore/labels/change","name":"change","color":"22694d"},{"url":"https://api.github.com/repos/jashkenas/underscore/labels/wontfix","name":"wontfix","color":"ffffff"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":23,"created_at":"2011-07-02T23:24:01Z","updated_at":"2015-02-10T22:45:17Z","closed_at":"2011-07-06T20:30:53Z","body":"When the template function needs an element that is not defined it returns an Error.\r\nIt would be great if the template function would act equivalent as if the value of the element is undefined\r\n\r\n\r\n    _.template('<%= foo %>', {foo: undefined});\r\n\r\nreturns \"\", but\r\n\r\n    _.template('<%= foo %>', {});\r\n\r\nreturns a ReferenceError\r\n\r\nThe 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.\r\nIn this case the application returns an error, it would be better if the template function would be more lax."},"comment":{"url":"https://api.github.com/repos/jashkenas/underscore/issues/comments/73800644","html_url":"https://github.com/jashkenas/underscore/issues/237#issuecomment-73800644","issue_url":"https://api.github.com/repos/jashkenas/underscore/issues/237","id":73800644,"user":{"login":"A-Tran","id":6796610,"avatar_url":"https://avatars.githubusercontent.com/u/6796610?v=3","gravatar_id":"","url":"https://api.github.com/users/A-Tran","html_url":"https://github.com/A-Tran","followers_url":"https://api.github.com/users/A-Tran/followers","following_url":"https://api.github.com/users/A-Tran/following{/other_user}","gists_url":"https://api.github.com/users/A-Tran/gists{/gist_id}","starred_url":"https://api.github.com/users/A-Tran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/A-Tran/subscriptions","organizations_url":"https://api.github.com/users/A-Tran/orgs","repos_url":"https://api.github.com/users/A-Tran/repos","events_url":"https://api.github.com/users/A-Tran/events{/privacy}","received_events_url":"https://api.github.com/users/A-Tran/received_events","type":"User","site_admin":false},"created_at":"2015-02-10T22:45:17Z","updated_at":"2015-02-10T22:45:17Z","body":"@nikmartin You don't actually have to write it as nested ifs, you can use the && statement! \r\n``` if (data.bar && data.bar.bat) { \r\n   ... do something\r\n}``` \r\nwhich I think is completely reasonable, given that the use case for this would be, if data contains the bar element (meaning bar may or may not be there), as would be the case with bat. And if your application suggests that maybe, `bat` is in every `bar`, and we are looking for a `foo` which might be in a `bat`, then its also safe to say\r\n``` if (data.bar && data.bar.bat.foo) {\r\n    ... do something\r\n}```\r\nWhich will only give you an undefined reference error if there is a case where `bar` exists and `bat` doesn't exist.\r\n\r\nAnd if you're used to other high-level programming languages where you can just say \r\n```if (data.bar.bat.foo) ``` and have that cover every case, and well, I'm no expert, but to check if foo is a property of bat I imagine you would need to have to be able to locate bat somewhere in memory, meaning that  the same sequential checking that you think is  'horrible' is required, whether it happens in the background or in your code. (? Not too certain about my statement in this last paragraph but fairly confident, would love to be corrected if I'm wrong)"}}