Search code examples
undefinedjquery-templates

jQuery tmpl: handling null/undefined elements


I have a data structure returned by a web service. It's a few levels deep, and could have null instead of the expected object. Which results in some ugly code to check.

<td>{{if FulfilledBy}}${FulfilledBy.Name}{{/if}}</td>

I can't change the output of the service, but I'd rather not need to check if FulfilledBy exists prior to access the .Name property.

Is there a better way to write this? I would have preferred something like

<td>${(FulfilledBy || {}).Name}</td>

but it doesn't work either.


Solution

  • According to docs, you can use expressions within ${}. Have you tried ${FulfilledBy? FulfilledBy.Name: 'no name'}?