From 8f167dc8b66af275704f3aec809eb0901c4847ba Mon Sep 17 00:00:00 2001 From: BorisMoore Date: Fri, 2 Jul 2010 18:31:07 -0700 Subject: [PATCH] Second stage of factoring into separate files - core minimal template functionality in jquery.tmpl.js, intended to enable primary templating scenarios - additional templating features or support for more advanced/less common scenarios in one or more additional files. Currently: jquery.tmplPlus.js. In this commit, jquery.tmplPlus.js contains support for the rendered event, and for the append pattern, which have been removed from jquery.tmpl.js. It also contains the tmplCmd plugin previously in jquery.tmplCmd.js Samples organized into folders based on whether they use only the core tmpl functionality, or additional tmplPlus functionality. --- README.md | 30 ++++++- .../{PagesTmplCore => PagesCore}/movies.html | 10 +-- demos/movies/PagesTmplPlus/movies1.html | 10 +-- demos/movies/PagesTmplPlus/movies2.html | 8 +- demos/movies/PagesTmplPlus/movies3.html | 8 +- .../basic.html | 75 ++++++++++++---- .../composition.html | 11 ++- .../{samplesAppend => samplesCore}/each.html | 0 .../parameters.html | 0 .../basic.html | 12 +-- .../composition.html | 1 + .../each.html | 1 + .../parameters.html | 1 + jquery.tmpl.js | 86 ++++++++----------- jquery.tmplPlus.js | 28 +++++- 15 files changed, 175 insertions(+), 106 deletions(-) rename demos/movies/{PagesTmplCore => PagesCore}/movies.html (98%) rename demos/{samplesAppendTo => samplesCore}/basic.html (50%) rename demos/{samplesAppendTo => samplesCore}/composition.html (91%) rename demos/{samplesAppend => samplesCore}/each.html (100%) rename demos/{samplesAppendTo => samplesCore}/parameters.html (100%) rename demos/{samplesAppend => samplesTmplPlus}/basic.html (81%) rename demos/{samplesAppend => samplesTmplPlus}/composition.html (97%) rename demos/{samplesAppendTo => samplesTmplPlus}/each.html (96%) rename demos/{samplesAppend => samplesTmplPlus}/parameters.html (97%) diff --git a/README.md b/README.md index 3331995..ad405b4 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,32 @@ A jQuery templating plugin - created for demonstration purposes. +____________________________________________________________________ - // Appends one LI, filled with data, into the UL - $("ul").append( tmpl, dataObject ); + // Render one LI, filled with data, then append it into the UL + $.tmpl( "
  • ${firstName}
  • ", dataObject ) + .appendTo( "ul" ); +____________________________________________________________________ - // Appends multiple LI, filled with data, into the UL - $("ul").append( tmpl, arrayOfDataObjects ); + + +____________________________________________________________________ + + // Render the declared template as one LI appended to the target UL + $( "#sometmpl" ) + .tmpl( dataObject ) + .appendTo( "ul" ); +____________________________________________________________________ + + // Render the declared template as multiple LIs appended to the target UL + // Provide a click event accessing the data + $( "#sometmpl" ) + .tmpl( arrayOfDataObjects ) + .appendTo( "ul" ) + .click( function() { + alert( $.tmpl(this).data.firstName ); + }); +____________________________________________________________________ A demo page using this plugin can be found here: http://infinity88.com/jquery-tmpl/movies/movies.htm \ No newline at end of file diff --git a/demos/movies/PagesTmplCore/movies.html b/demos/movies/PagesCore/movies.html similarity index 98% rename from demos/movies/PagesTmplCore/movies.html rename to demos/movies/PagesCore/movies.html index 79cfd11..b214104 100644 --- a/demos/movies/PagesTmplCore/movies.html +++ b/demos/movies/PagesCore/movies.html @@ -125,9 +125,9 @@

    Netflix: Book a Movie...

    $( "#cartTmpl" ) .tmpl( cart ) .appendTo( ".cart", cart ); - - var cartCtx = $.tmpl( ".cart td" ); - + + var cartCtx = $( ".cart td" ).tmpl(); + function selectGenre() { $( "#genres li" ).removeClass( "selected" ); $( this ).addClass( "selected" ); @@ -293,7 +293,7 @@

    Netflix: Book a Movie...

    return; } var ctx = bookingCtxs[booking.movie.Id], - tmpl = $.tmpl(edit ? "#bookingEditTmpl" : "#bookingTitleTmpl"); + tmpl = $( edit ? "#bookingEditTmpl" : "#bookingTitleTmpl" ).tmpl(); if ( ctx.tmpl !== tmpl) { ctx.tmpl = tmpl; updateContext( ctx ); @@ -339,7 +339,7 @@

    Netflix: Book a Movie...

    function updateContext( ctx ) { var coll = ctx.nodes; - $( coll[0] ).before( ctx); + $.tmpl( null, null, null, ctx ).insertBefore( coll[0] ); $( coll ).remove(); } diff --git a/demos/movies/PagesTmplPlus/movies1.html b/demos/movies/PagesTmplPlus/movies1.html index 3a826ec..1e8dad4 100644 --- a/demos/movies/PagesTmplPlus/movies1.html +++ b/demos/movies/PagesTmplPlus/movies1.html @@ -123,13 +123,13 @@

    Netflix: Book a Movie...

    removeBookings(); }) .empty(); - + $( "#cartTmpl" ) .tmpl( cart ) .appendTo( ".cart", cart ); - - var cartCtx = $.tmpl( ".cart td" ); - + + var cartCtx = $( ".cart td" ).tmpl(); + function selectGenre() { $( "#genres li" ).removeClass( "selected" ); $( this ).addClass( "selected" ); @@ -293,7 +293,7 @@

    Netflix: Book a Movie...

    return; } var ctx = bookingCtx( booking ), - tmpl = $.tmpl(edit ? "#bookingEditTmpl" : "#bookingTitleTmpl"); + tmpl = $( edit ? "#bookingEditTmpl" : "#bookingTitleTmpl" ).tmpl(); if ( ctx.tmpl !== tmpl) { ctx.tmpl = tmpl; $.tmplCmd( "update", ctx ); diff --git a/demos/movies/PagesTmplPlus/movies2.html b/demos/movies/PagesTmplPlus/movies2.html index 37f73e1..e9e9dcc 100644 --- a/demos/movies/PagesTmplPlus/movies2.html +++ b/demos/movies/PagesTmplPlus/movies2.html @@ -125,9 +125,9 @@

    Netflix: Book a Movie...

    $( "#cartTmpl" ) .tmpl( cart ) .appendTo( ".cart", cart ); - - var cartCtx = $.tmpl( ".cart td" ); - + + var cartCtx = $( ".cart td" ).tmpl(); + function selectGenre() { $( "#genres li" ).removeClass( "selected" ); $( this ).addClass( "selected" ); @@ -249,7 +249,7 @@

    Netflix: Book a Movie...

    var ctx = bookingCtx( booking ), rendered = edit ? onBookingEditRendered : onBookingRendered; if ( ctx.rendered !== rendered) { - ctx.tmpl = edit ? "#bookingEditTmpl" : "#bookingTitleTmpl"; + ctx.tmpl = $( edit ? "#bookingEditTmpl" : "#bookingTitleTmpl" ).tmpl(); ctx.rendered = rendered; $.tmplCmd( "update", ctx ); } diff --git a/demos/movies/PagesTmplPlus/movies3.html b/demos/movies/PagesTmplPlus/movies3.html index 35b5be4..0167a95 100644 --- a/demos/movies/PagesTmplPlus/movies3.html +++ b/demos/movies/PagesTmplPlus/movies3.html @@ -123,9 +123,9 @@

    Netflix: Book a Movie...

    }) .empty() .append( "#cartTmpl", cart ); - - var cartCtx = $.tmpl( ".cart td" ); - + + var cartCtx = $( ".cart td" ).tmpl(); + function selectGenre() { $( "#genres li" ).removeClass( "selected" ); $( this ).addClass( "selected" ); @@ -242,7 +242,7 @@

    Netflix: Book a Movie...

    var ctx = bookingCtx( booking ), rendered = edit ? onBookingEditRendered : onBookingRendered; if ( ctx.rendered !== rendered) { - ctx.tmpl = edit ? "#bookingEditTmpl" : "#bookingTitleTmpl"; + ctx.tmpl = $( edit ? "#bookingEditTmpl" : "#bookingTitleTmpl" ).tmpl(); ctx.rendered = rendered; $.tmplCmd( "update", ctx ); } diff --git a/demos/samplesAppendTo/basic.html b/demos/samplesCore/basic.html similarity index 50% rename from demos/samplesAppendTo/basic.html rename to demos/samplesCore/basic.html index d1ccdad..43c528e 100644 --- a/demos/samplesAppendTo/basic.html +++ b/demos/samplesCore/basic.html @@ -1,6 +1,12 @@ + @@ -87,7 +125,6 @@ ${firstName} ${lastName}
    - -
    +
    Target
    \ No newline at end of file diff --git a/demos/samplesAppendTo/composition.html b/demos/samplesCore/composition.html similarity index 91% rename from demos/samplesAppendTo/composition.html rename to demos/samplesCore/composition.html index 4510b7c..c40d381 100644 --- a/demos/samplesAppendTo/composition.html +++ b/demos/samplesCore/composition.html @@ -53,10 +53,13 @@ } function getTemplate( key ) { - return $.tmpl("#tmpl" + key); + return $("#tmpl" + key).tmpl(); } $(function(){ + // Create named template, to be used in composition below + $.templates.citySeparator = $.tmpl( '' ); + $("#tmplPeople") .tmpl( people ) .appendTo(".peopleTable"); @@ -78,12 +81,8 @@ - - diff --git a/demos/samplesAppend/each.html b/demos/samplesCore/each.html similarity index 100% rename from demos/samplesAppend/each.html rename to demos/samplesCore/each.html diff --git a/demos/samplesAppendTo/parameters.html b/demos/samplesCore/parameters.html similarity index 100% rename from demos/samplesAppendTo/parameters.html rename to demos/samplesCore/parameters.html diff --git a/demos/samplesAppend/basic.html b/demos/samplesTmplPlus/basic.html similarity index 81% rename from demos/samplesAppend/basic.html rename to demos/samplesTmplPlus/basic.html index a49c566..9d4a381 100644 --- a/demos/samplesAppend/basic.html +++ b/demos/samplesTmplPlus/basic.html @@ -2,7 +2,8 @@ This sample illustrates some basic templating features and scenarios. --> - + + - -
    +
    Target
    diff --git a/demos/samplesAppend/composition.html b/demos/samplesTmplPlus/composition.html similarity index 97% rename from demos/samplesAppend/composition.html rename to demos/samplesTmplPlus/composition.html index 311820c..d7910d4 100644 --- a/demos/samplesAppend/composition.html +++ b/demos/samplesTmplPlus/composition.html @@ -12,6 +12,7 @@ + + +