Suppose you have:
var html = myTemplate.render(myOrder);
and your template is:
{{:shipping.id}}
Here are the results with the different versions of myOrder:
myOrder = {shipping: {name: "Jo", id: "J1"} }
-> html: "J1
myOrder = {shipping: {name: "Jo"} }
-> html: ""
myOrder = {}
-> html: "{Error: TypeError: Unable to get property 'id' of undefined or null reference}"
So now, here are several ways to handle that last case - without outputting the error message:
1) Use onerror=... on the {{:}} tag to specify a fallback rendering of the tag in the case of error.
For example if you want to render the empty string when the shipping object is null or undefined, you can use the template:
{{:shipping.id onerror=''}}
Or you could write
{{:shipping.id onerror='no shipping info'}}
2) Test for the shipping object using {{if}} or {{if}} {{else}} {{/if}}
{{if shipping}}{{:shipping.id}}{{else}}no shipping info{{/if}}
3) Use {{for}} or {{for}} {{else}} {{/for}}
{{for shipping}}{{:id}}{{else}}no shipping info{{/for}}
4) Use a null check
{{:shipping && shipping.id}}
5) Use a ternary expression
{{:shipping ? shipping.id : 'no shipping info'}}
So to summarize, here is a template showing all of these alternatives:
Template
<script id="myTmpl" type="text/x-jsrender">
1 {{:shipping.id onerror='no shipping info'}}<br/>
2 {{if shipping}}{{:shipping.id}}{{else}}no shipping info{{/if}}<br />
3 {{for shipping}}{{:id}}{{else}}no shipping info{{/for}}<br />
4 {{:shipping && shipping.id}}<br />
5 {{:shipping ? shipping.id : 'no shipping info'}}<br />
</script>
Script
var myOrder = {};
var html = myTemplate.render(myOrder);
Output:
1 no shipping info
2 no shipping info
3 no
shipping info
4
5 no shipping info
Finally, if the order itself is null or undefined, or if you pass an array of orders, but some may be undefined, then you can wrap the whole template by an {{if #data}} or equivalently simply {{if}}, which tests for whether the current object, (the contextual data object that you are rendering this template against) is null.
Template
<script id="myTmpl" type="text/x-jsrender">
{{if}}
{{:shipping.id onerror='no shipping info'}}<br/>
{{else}}
no order<br/>
{{/if}}
</script>
Script
var myOrders = [
{shipping: {id: "J1"}},
,
{},
{shipping: {id: "J2"}},
];
var html = myTemplate.render(myOrders)
Output:
J1
no order
no shipping info
J2