Search code examples
javascriptjquerytemplate-enginejsrenderjsviews

How to check array null or empty in jsrender?


This is My Code below

<script type="text/javascript">
			
		var test1 = {
				"type" : "success",
				"code" : "600",
				"data" : [ ]
			};
		
		var template = $.templates("#template");
		var htmlOutput = template.render(test1);
		$("#billListBox").html(htmlOutput);
		
		function billButtonOnClick(searchBillId,isComeFromBillClick,billDiv){
			console.log(searchBillId);
			console.log(isComeFromBillClick);
			console.log(billDiv);
		}

	</script>
<div id="billListBox"></div> 

I want to check data is empty or null in jsrender? how to check ?


Solution

  • If you are using {{for data}} then you don't actually need to test for empty/null (whether in code or by wrapping in {{if}}...{{/if}}).

    You can simply write

    {{for data}}
        ...
    {{/for}}
    

    and that will work fine even if it is empty or null - it will just render nothing for that block.

    Or if you want you can use

    {{for data}}
        ...
    {{else}}
        output this if data is empty or null
    {{/for}}
    

    and it will output whatever you want for the null/empty case.

    See http://www.jsviews.com/#fortag