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