MCPcopy Create free account
hub / github.com/BorisMoore/jquery-tmpl / build

Function build

jquery.tmpl.js:281–314  ·  view source on GitHub ↗
( tmplItem, nested, content )

Source from the content-addressed store, hash-verified

279 //========================== Private helper functions, used by code above ==========================
280
281 function build( tmplItem, nested, content ) {
282 // Convert hierarchical content into flat string array
283 // and finally return array of fragments ready for DOM insertion
284 var frag, ret = content ? jQuery.map( content, function( item ) {
285 return (typeof item === "string") ?
286 // Insert template item annotations, to be converted to jQuery.data( "tmplItem" ) when elems are inserted into DOM.
287 (tmplItem.key ? item.replace( /(<\w+)(?=[\s>])(?![^>]*_tmplitem)([^>]*)/g, "$1 " + tmplItmAtt + "=\"" + tmplItem.key + "\" $2" ) : item) :
288 // This is a child template item. Build nested template.
289 build( item, tmplItem, item._ctnt );
290 }) :
291 // If content is not defined, insert tmplItem directly. Not a template item. May be a string, or a string array, e.g. from {{html $item.html()}}.
292 tmplItem;
293 if ( nested ) {
294 return ret;
295 }
296
297 // top-level template
298 ret = ret.join("");
299
300 // Support templates which have initial or final text nodes, or consist only of text
301 // Also support HTML entities within the HTML markup.
302 ret.replace( /^\s*([^<\s][^<]*)?(<[\w\W]+>)([^>]*[^>\s])?\s*$/, function( all, before, middle, after) {
303 frag = jQuery( middle ).get();
304
305 storeTmplItems( frag );
306 if ( before ) {
307 frag = unencode( before ).concat(frag);
308 }
309 if ( after ) {
310 frag = frag.concat(unencode( after ));
311 }
312 });
313 return frag ? frag : unencode( ret );
314 }
315
316 function unencode( text ) {
317 // Use createElement, since createTextNode will not render HTML entities correctly

Callers 2

jquery.tmpl.jsFile · 0.85
updateWrappedFunction · 0.85

Calls 2

storeTmplItemsFunction · 0.85
unencodeFunction · 0.85

Tested by

no test coverage detected