From: Tim Düsterhus Date: Mon, 15 Apr 2013 13:32:11 +0000 (+0200) Subject: Add {if} to WCF.Template X-Git-Tag: 2.0.0_Beta_1~314^2~7 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=0f5aa6153f5eab8dd69c063ac3cbe81f05c951f7;p=GitHub%2FWoltLab%2FWCF.git Add {if} to WCF.Template --- diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index c8447d76d2..cf2c759677 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -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; }, /**