I use JsRender .
There are 2 options as render elements.
First: when I get the Ajax data in json
format, I substitute values in the markers.
Second: when I just render a new item without data. So at this stage I get an error.
How correctly to set a condition that a template engine substituted values when it is?
<script id="holidaysFormTpl" type="text/x-jsrender"> <div class="tr for-clone item active"> <div class="td parent"> <div class="wrapp-input edit"> <input type="text" class="input-val" name="name" placeholder="Christmas" value="{{if name}} {{:name}} {{/if}}"> </div> </div> <div class="td parent"> <div class="wrapp-input edit"> <input type="text" id="choose_date" name="start_date" value="{{:start_date}}" class="date-pick" placeholder="Choose date"> </div> </div> <div class="td right-align"> <a href="#" class="btn waves-effect waves-light ml-10 small save-this-row">Save it</a> </div> <div class="td right-align"> <a href="#" class="trash-ico "><span class="ico icon-trash "></span></a> </div> </div> </script>
//GENERATE DINAMIC HOLIDAYS BLOCK $('.add-hol').click(function(){ var holidaysForm = $('#holidaysFormTpl').render(); $(this).parents('.parent-form').find('.display-table .tbody').append(holidaysForm); initializeDtepicker2(); }); //GET HOLIDAYS $('#dep-box').on('click', '.show-holidays', function(){ var btn = $(this); var id = btn.parents('form').data('dep-id'); $.ajax({ url : '/settings/get_holidays', method: 'POST', dataType: 'json', data: { id : id }, success: function(data){ var holidaysForm = $('#holidaysFormTpl').render(data); $('.holidays-box .display-table .tbody').append(holidaysForm); initializeDtepicker2(); } }); });
Uncaught TypeError: Cannot read property 'name' of undefined