Add {if} to WCF.Template
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Apr 2013 13:32:11 +0000 (15:32 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Apr 2013 13:32:11 +0000 (15:32 +0200)
wcfsetup/install/files/js/WCF.js

index c8447d76d2976bc967bb162fb142d5004ba234b8..cf2c759677ebb773f415156957189f17a691eb10 100755 (executable)
@@ -3242,22 +3242,35 @@ WCF.Template = Class.extend({
                var self = this;
                
                // parse our variable-tags
-               this._template = this._template.replace(/\{\$(.*?)\}/g, function ($match) {
-                       var $name = $match.substring(2, $match.length - 1);
-                       self._neededVars.push($name);
+               this._template = this._template.replace(/\{\$(.+?)\}/g, function (_, name) {
+                       self._neededVars.push(name);
                        
-                       return "' + WCF.String.escapeHTML(v."+ $name + ") + '";
-               }).replace(/\{#\$(.*?)\}/g, function ($match) {
-                       var $name = $match.substring(3, $match.length - 1);
-                       self._neededVars.push($name);
+                       return "' + WCF.String.escapeHTML(v."+ name + ") + '";
+               }).replace(/\{#\$(.+?)\}/g, function (_, name) {
+                       self._neededVars.push(name);
                        
-                       return "' + WCF.String.formatNumeric(v."+ $name + ") + '";
-               }).replace(/\{@\$(.*?)\}/g, function ($match) {
-                       var $name = $match.substring(3, $match.length - 1);
-                       self._neededVars.push($name);
+                       return "' + WCF.String.formatNumeric(v."+ name + ") + '";
+               }).replace(/\{@\$(.+?)\}/g, function (_, name) {
+                       self._neededVars.push(name);
                        
-                       return "' + (v."+ $name + ") + '";
-               });
+                       return "' + (v."+ name + ") + '";
+               }).replace(/{if (.+?)}/g, function (_, content) {
+                       content = content.replace(/\$([^\s]+)/g, function (_, name) {
+                               self._neededVars.push(name);
+                       
+                               return "v." + name;
+                       });
+                       
+                       return "'; if (" + content + ") { $output += '";
+               }).replace(/{elseif (.+?)}/g, function (_, content) {
+                       content = content.replace(/\$([^\s]+)/g, function (_, name) {
+                               self._neededVars.push(name);
+                       
+                               return "v." + name;
+                       });
+                       
+                       return "'; } else if (" + content + ") { $output += '";
+               }).replace(/{else}/g, "'; } else { $output += '").replace(/{\/if}/g, "'; } $output += '");
                
                // insert delimiter tags
                this._template = this._template.replace('{ldelim}', '{').replace('{rdelim}', '}');
@@ -3265,7 +3278,7 @@ WCF.Template = Class.extend({
                // escape newlines
                this._template = this._template.replace(/(\r\n|\n|\r)/g, '\\n');
                
-               this._template = "'" + this.insertLiterals(this._template) + "';";
+               this._template = "$output += '" + this.insertLiterals(this._template) + "';";
        },
        
        /**
@@ -3277,12 +3290,16 @@ WCF.Template = Class.extend({
        fetch: function(v) {
                // check whether all needed variables are given
                for (var $i = 0; $i < this._neededVars.length; $i++) {
-                       if (!v[this._neededVars[$i]]) {
+                       if (typeof v[this._neededVars[$i]] === 'undefined') {
                                throw new Error('Use of undefined variable ' + this._neededVars[$i]);
                        }
                }
                
-               return eval(this._template);
+               var $output = '';
+               
+               eval(this._template);
+               
+               return $output;
        },
        
        /**