Fix server side rendering with PHP 5.3. re #82

This commit is contained in:
Sébastien Lucas 2013-09-03 21:09:48 +02:00
parent c0d54591eb
commit 6105de79b2

View file

@ -10,24 +10,25 @@
class doT { class doT {
public $functionBody; public $functionBody;
private $functionCode; private $functionCode;
private $def; public $def;
private function resolveDefs ($block) { public function resolveDefs ($block) {
return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/", function ($m) { $me = $this;
return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/", function ($m) use ($me) {
$d = $m[1]; $d = $m[1];
$d = substr ($d, 4); $d = substr ($d, 4);
if (!array_key_exists ($d, $this->def)) { if (!array_key_exists ($d, $me->def)) {
return ""; return "";
} }
if (preg_match ("/\{\{#([\s\S]+?)\}\}/", $this->def [$d])) { if (preg_match ("/\{\{#([\s\S]+?)\}\}/", $me->def [$d])) {
return $this->resolveDefs ($this->def [$d], $this->def); return $me->resolveDefs ($me->def [$d], $me->def);
} else { } else {
return $this->def [$d]; return $me->def [$d];
} }
}, $block); }, $block);
} }
private function handleDotNotation ($string) { public function handleDotNotation ($string) {
$out = preg_replace ("/(\w+)\.(.*?)([\s,\)])/", "\$$1[\"$2\"]$3", $string); $out = preg_replace ("/(\w+)\.(.*?)([\s,\)])/", "\$$1[\"$2\"]$3", $string);
$out = preg_replace ("/(\w+)\.([\w\.]*?)$/", "\$$1[\"$2\"] ", $out); $out = preg_replace ("/(\w+)\.([\w\.]*?)$/", "\$$1[\"$2\"] ", $out);
$out = preg_replace ("/\./", '"]["', $out); $out = preg_replace ("/\./", '"]["', $out);
@ -38,6 +39,7 @@ class doT {
} }
public function template ($string, $def) { public function template ($string, $def) {
$me = $this;
$func = preg_replace ("/'|\\\/", "\\$&", $string); $func = preg_replace ("/'|\\\/", "\\$&", $string);
// deps // deps
@ -48,34 +50,34 @@ class doT {
$func = $this->resolveDefs ($func); $func = $this->resolveDefs ($func);
} }
// interpolate // interpolate
$func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/", function ($m) { $func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/", function ($m) use ($me) {
return "' . " . $this->handleDotNotation ($m[1]) . " . '"; return "' . " . $me->handleDotNotation ($m[1]) . " . '";
}, $func); }, $func);
// Conditional // Conditional
$func = preg_replace_callback ("/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/", function ($m) { $func = preg_replace_callback ("/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/", function ($m) use ($me) {
$elsecase = $m[1]; $elsecase = $m[1];
$code = $m[2]; $code = $m[2];
if ($elsecase) { if ($elsecase) {
if ($code) { if ($code) {
return "';} else if (" . $this->handleDotNotation ($code) . ") { $" . "out.='"; return "';} else if (" . $me->handleDotNotation ($code) . ") { $" . "out.='";
} else { } else {
return "';} else { $" . "out.='"; return "';} else { $" . "out.='";
} }
} else { } else {
if ($code) { if ($code) {
return "'; if (" . $this->handleDotNotation ($code) . ") { $" . "out.='"; return "'; if (" . $me->handleDotNotation ($code) . ") { $" . "out.='";
} else { } else {
return "';} $" . "out.='"; return "';} $" . "out.='";
} }
} }
}, $func); }, $func);
// Iterate // Iterate
$func = preg_replace_callback ("/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/", function ($m) { $func = preg_replace_callback ("/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/", function ($m) use ($me) {
if (count($m) > 1) { if (count($m) > 1) {
$iterate = $m[1]; $iterate = $m[1];
$vname = $m[2]; $vname = $m[2];
$iname = $m[3]; $iname = $m[3];
$iterate = $this->handleDotNotation ($iterate); $iterate = $me->handleDotNotation ($iterate);
return "'; for (\$$iname = 0; \$$iname < count($iterate); \$$iname++) { \$$vname = $iterate [\$$iname]; \$out.='"; return "'; for (\$$iname = 0; \$$iname < count($iterate); \$$iname++) { \$$vname = $iterate [\$$iname]; \$out.='";
} else { } else {
return "';} $" . "out.='"; return "';} $" . "out.='";