From fe9d7e4fe7659fdcaae686a391597ff6dd730cbd Mon Sep 17 00:00:00 2001 From: BorisMoore Date: Fri, 4 Feb 2011 14:10:37 -0800 Subject: [PATCH] Changed the variable used in buildTmplFn from _ to __. (Issue 10) Compromise choice: a variable name that is unlikely to exist as a global, or as a field name on data, but is not too long, to keep code size down. --- jquery.tmpl.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/jquery.tmpl.js b/jquery.tmpl.js index c37e764..653c048 100644 --- a/jquery.tmpl.js +++ b/jquery.tmpl.js @@ -207,15 +207,15 @@ tag: { "tmpl": { _default: { $2: "null" }, - open: "if($notnull_1){_=_.concat($item.nest($1,$2));}" + open: "if($notnull_1){__=__.concat($item.nest($1,$2));}" // tmpl target parameter can be of type function, so use $1, not $1a (so not auto detection of functions) // This means that {{tmpl foo}} treats foo as a template (which IS a function). // Explicit parens can be used if foo is a function that returns a template: {{tmpl foo()}}. }, "wrap": { _default: { $2: "null" }, - open: "$item.calls(_,$1,$2);_=[];", - close: "call=$item.calls();_=call._.concat($item.wrap(call,_));" + open: "$item.calls(__,$1,$2);__=[];", + close: "call=$item.calls();__=call._.concat($item.wrap(call,__));" }, "each": { _default: { $2: "$index, $value" }, @@ -232,12 +232,12 @@ }, "html": { // Unecoded expression evaluation. - open: "if($notnull_1){_.push($1a);}" + open: "if($notnull_1){__.push($1a);}" }, "=": { // Encoded expression evaluation. Abbreviated form is ${}. _default: { $1: "$data" }, - open: "if($notnull_1){_.push($.encode($1a));}" + open: "if($notnull_1){__.push($.encode($1a));}" }, "!": { // Comment tag. Skipped by parser @@ -314,10 +314,11 @@ // Generate a reusable function that will serve to render a template against data function buildTmplFn( markup ) { return new Function("jQuery","$item", - "var $=jQuery,call,_=[],$data=$item.data;" + + // Use the variable __ to hold a string array while building the compiled template. (See https://github.com/jquery/jquery-tmpl/issues#issue/10). + "var $=jQuery,call,__=[],$data=$item.data;" + // Introduce the data as local variables using with(){} - "with($data){_.push('" + + "with($data){__.push('" + // Convert the template into pure JavaScript jQuery.trim(markup) @@ -352,9 +353,9 @@ .split( "$1a" ).join( exprAutoFnDetect ) .split( "$1" ).join( expr ) .split( "$2" ).join( fnargs || def.$2 || "" ) + - "_.push('"; + "__.push('"; }) + - "');}return _;" + "');}return __;" ); } function updateWrapped( options, wrapped ) {