underscore.js - underscore template vs javascript html -
for medialibrary i've been using underscore javascript library append json data _.template
function. media items loaded can filter data later on search function. that's it's basicly ment right?
but i've used other items like: buttons, attachments, etc. data created on fly in javascript when select media items. after click on insert button underscore appends small snippets.
is little much? should actualy use html strings inside javascript that? have 7 underscore templates small snippets user actions don't realy need underscore templating....
i'm big fan of templates (underscore/mustache.. etc) might little bias. alternative have js string , append things it, thats archaic. or have jquery build structure, thats hard on eyes -- @ least on mine.
this me better:
<div class="<%= classtype %>"> <label id="<%= id %>"><%= name %></label> <input name="<%= name %>" id="<%= id %>" value="<%= value %>"> </div>
than this:
var template = ['<div class="', classtype, '"><label id="', id, '">', name, '</label><input name="', name, '" id="', id, '" value="', value, '"></div>'].join('') // or god forbid!!! var template = '<div class="' + classtype + '"><label id="' + id + '">' + name + '</label>'<input name="' + name + '" id="' + id + '" value="' + value + '"></div>';
or this:
// please dont ever this, know when write it makes lot // of sense, trust me, when come month later... no doesnt $('<div>').addclass(classtype).append( $('<label>').attr('id', id).html(name) ).append( $('<input>').attr({'name': name, 'id': id, 'value': value}) )
if have repeats more once say, sure, add template. problem last 2 examples project inevitably grow, , you'll find copy , pasting "just 1 more time" satisfy requirement thing "just once".. never happens. entire professional career plagued "this used once" code. -- im rambling.
yes. use templates! readability reason enough. save sooo time debugging.
Comments
Post a Comment