Skip to content

Commit

Permalink
Added support for elseif semantics, and a new comment tag.
Browse files Browse the repository at this point in the history
For if, else if, else use: {{if a}}...{{else b}}...{{else}}...{{/if}}
For comments within templates, use {{! Here is my comment }}.
Added a new sample: samplesCore/condition.html, to illustrate use of these
template tags.
  • Loading branch information
BorisMoore committed Sep 4, 2010
1 parent 19f6ba3 commit 7769e2b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
43 changes: 43 additions & 0 deletions demos/samplesCore/conditional.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
This sample illustrates use of conditional template tags {{if}} and {{else}}.
The comment tag {{! }} is also shown.
-->

<script src="http://code.jquery.com/jquery.js"></script>
<script src="../../jquery.tmpl.js" type="text/javascript"></script>
<script>
$(function(){
$( "#movieTemplate" )
.tmpl( movies )
.appendTo( "#movieList" );
});

var movies = [
{ Name: "Meet Joe Black", Languages: "French", Subtitles: "English" },
{ Name: "The Mighty", Subtitles: "French and Spanish" },
{ Name: "The Mighty" },
{ Name: "City Hunter", Languages: "Mandarin and Cantonese" }
];
</script>

<script id="movieTemplate" type="text/x-jquery-tmpl">
<li>${Name}

{{if Languages}}

(Alternative languages: ${Languages}).

{{else Subtitles}} {{! Note that the 'else' tag with a parameter behaves as 'else if' }}

(Original language only. Subtitles in ${Subtitles}).

{{else}} {{! The 'else' tag with no parameter behaves as 'else' }}

(Original version only, without subtitles).

{{/if}}

</li>
</script>

<ul id="movieList"></ul>
15 changes: 11 additions & 4 deletions jquery.tmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function( jQuery, undefined ){
var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]*(<[\w\W]+>)[^>]*$/,
var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]*(<[\w\W]+>)[^>]*$|{{! /,
newTmplItems = {}, wrappedItems = {}, appendToTmplItems, topTmplItem = { key: 0, data: {} }, itemKey = 0, cloneIndex = 0, stack = [];

function newTmplItem( options, parentItem, fn, data ) {
Expand Down Expand Up @@ -226,14 +226,21 @@
close: "}"
},
"else": {
open: "}else{"
_default: { $1: "true" },
open: "}else if(($notnull_1) && $1a){"
},
"html": {
// Unecoded expression evaluation.
open: "if($notnull_1){_.push($1a);}"
},
"=": {
// Encoded expression evaluation. Abbreviated form is ${}.
_default: { $1: "$data" },
open: "if($notnull_1){_.push($.encode($1a));}"
},
"!": {
// Comment tag. Skipped by parser
open: ""
}
},

Expand Down Expand Up @@ -331,12 +338,12 @@
expr = parens ? (target.indexOf(".") > -1 ? target + parens : ("(" + target + ").call($item" + args)) : target;
exprAutoFnDetect = parens ? expr : "(typeof(" + target + ")==='function'?(" + target + ").call($item):(" + target + "))";
} else {
expr = def["$1"] || "null";
exprAutoFnDetect = expr = def["$1"] || "null";
}
fnargs = unescape( fnargs );
return "');" +
tag[ slash ? "close" : "open" ]
.split( "$notnull_1" ).join( "typeof(" + target + ")!=='undefined' && (" + target + ")!=null" )
.split( "$notnull_1" ).join( target ? "typeof(" + target + ")!=='undefined' && (" + target + ")!=null" : "true" )
.split( "$1a" ).join( exprAutoFnDetect )
.split( "$1" ).join( expr )
.split( "$2" ).join( fnargs ?
Expand Down

0 comments on commit 7769e2b

Please sign in to comment.