Upgrade doT-php to fix the problems with bootstrap template. fix #171

This commit is contained in:
Sébastien Lucas 2014-08-27 10:02:42 +02:00
parent 7ae6041c29
commit a3b29a11a1

View file

@ -1,6 +1,6 @@
<?php <?php
/** /**
* PHP renderer for doT templating engine * PHP renderer for doT templating engine
* *
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <sebastien@slucas.fr> * @author Sébastien Lucas <sebastien@slucas.fr>
@ -11,7 +11,7 @@ class doT {
public $functionBody; public $functionBody;
private $functionCode; private $functionCode;
public $def; public $def;
public function resolveDefs ($block) { public function resolveDefs ($block) {
$me = $this; $me = $this;
return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/", function ($m) use ($me) { return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/", function ($m) use ($me) {
@ -27,20 +27,21 @@ class doT {
} }
}, $block); }, $block);
} }
public 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);
// Special hideous case : shouldn't be committed // Special hideous case : shouldn't be committed
$out = preg_replace ("/^i /", ' $i ', $out); $out = preg_replace ("/^i /", ' $i ', $out);
return $out; return $out;
} }
public function template ($string, $def) { public function template ($string, $def) {
$me = $this; $me = $this;
$func = preg_replace ("/'|\\\/", "\\$&", $string);
$func = $string;
// deps // deps
if (empty ($def)) { if (empty ($def)) {
@ -49,6 +50,9 @@ class doT {
$this->def = $def; $this->def = $def;
$func = $this->resolveDefs ($func); $func = $this->resolveDefs ($func);
} }
$func = preg_replace ("/'|\\\/", "\\$&", $func);
// interpolate // interpolate
$func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/", function ($m) use ($me) { $func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/", function ($m) use ($me) {
return "' . " . $me->handleDotNotation ($m[1]) . " . '"; return "' . " . $me->handleDotNotation ($m[1]) . " . '";
@ -77,20 +81,19 @@ class doT {
$iterate = $m[1]; $iterate = $m[1];
$vname = $m[2]; $vname = $m[2];
$iname = $m[3]; $iname = $m[3];
$iterate = $me->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.='";
} }
}, $func); }, $func);
$func = '$out = \'' . $func . '\'; return $out;'; $func = '$out = \'' . $func . '\'; return $out;';
$this->functionBody = $func; $this->functionBody = $func;
//$this->functionCode = create_function ('$it', $func);
return create_function ('$it', $func); return create_function ('$it', $func);
} }
public function execute ($data) { public function execute ($data) {
return $this->functionCode ($data); return $this->functionCode ($data);
} }