Add a PHP renderer for doT templates ... does not handle all cases (far from it) but it's enought for COPS

This commit is contained in:
Sébastien Lucas 2013-08-21 15:41:20 +02:00
parent 7e5841e265
commit a2384ce7cc

96
resources/doT-php/doT.php Normal file
View file

@ -0,0 +1,96 @@
<?php
/**
* PHP renderer for doT templating engine
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <sebastien@slucas.fr>
*/
class doT {
public $functionBody;
private $functionCode;
private $def;
private function resolveDefs ($block) {
return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/", function ($m) {
$d = $m[1];
$d = substr ($d, 4);
if (!array_key_exists ($d, $this->def)) {
return "";
}
if (preg_match ("/\{\{#([\s\S]+?)\}\}/", $this->def [$d])) {
return $this->resolveDefs ($this->def [$d], $this->def);
} else {
return $this->def [$d];
}
}, $block);
}
private function handleDotNotation ($string) {
$out = preg_replace ("/(\w+)\.(.*?)([\s,\)])/", "\$$1[\"$2\"]$3", $string);
$out = preg_replace ("/(\w+)\.([\w\.]*?)$/", "\$$1[\"$2\"] ", $out);
$out = preg_replace ("/\./", '"]["', $out);
// Special hideous case : shouldn't be committed
$out = preg_replace ("/^i /", ' $i ', $out);
return $out;
}
public function template ($string, $def) {
$func = preg_replace ("/'|\\\/", "\\$&", $string);
// deps
if (empty ($def)) {
$func = preg_replace ("/\{\{#([\s\S]+?)\}\}/", "", $func);
} else {
$this->def = $def;
$func = $this->resolveDefs ($func);
}
// interpolate
$func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/", function ($m) {
return "' . " . $this->handleDotNotation ($m[1]) . " . '";
}, $func);
// Conditional
$func = preg_replace_callback ("/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/", function ($m) {
$elsecase = $m[1];
$code = $m[2];
if ($elsecase) {
if ($code) {
return "';} else if (" . $this->handleDotNotation ($code) . ") { $" . "out.='";
} else {
return "';} else { $" . "out.='";
}
} else {
if ($code) {
return "'; if (" . $this->handleDotNotation ($code) . ") { $" . "out.='";
} else {
return "';} $" . "out.='";
}
}
}, $func);
// Iterate
$func = preg_replace_callback ("/\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/", function ($m) {
if (count($m) > 1) {
$iterate = $m[1];
$vname = $m[2];
$iname = $m[3];
$iterate = $this->handleDotNotation ($iterate);
return "'; for (\$$iname = 0; \$$iname < count($iterate); \$$iname++) { \$$vname = $iterate [\$$iname]; \$out.='";
} else {
return "';} $" . "out.='";
}
}, $func);
$func = '$out = \'' . $func . '\'; return $out;';
$this->functionBody = $func;
//$this->functionCode = create_function ('$it', $func);
return create_function ('$it', $func);
}
public function execute ($data) {
return $this->functionCode ($data);
}
}