Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions doT.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

class doT {
public $functionBody;
private $functionCode;
public $def;
public $argName = '$it';

public function resolveDefs ($block) {
$me = $this;
return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/", function ($m) use ($me) {
return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/u", function ($m) use ($me) {
$d = $m[1];
$d = substr ($d, 4);
if (!array_key_exists ($d, $me->def)) {
return "";
}
if (preg_match ("/\{\{#([\s\S]+?)\}\}/", $me->def [$d])) {
if (preg_match ("/\{\{#([\s\S]+?)\}\}/u", $me->def [$d])) {
return $me->resolveDefs ($me->def [$d], $me->def);
} else {
return $me->def [$d];
Expand All @@ -29,36 +29,36 @@ public function resolveDefs ($block) {
}

public function handleDotNotation ($string) {
$out = preg_replace ("/(\w+)\.(.*?)([\s,\)])/", "\$$1[\"$2\"]$3", $string);
$out = preg_replace ("/(\w+)\.([\w\.]*?)$/", "\$$1[\"$2\"] ", $out);
$out = preg_replace ("/\./", '"]["', $out);
$out = preg_replace ("/(\w+)\.(.*?)([\s,\)])/u", "\$$1[\"$2\"]$3", $string);
$out = preg_replace ("/(\w+)\.([\w\.]*?)$/u", "\$$1[\"$2\"] ", $out);
$out = preg_replace ("/\./u", '"]["', $out);

// Special hideous case : shouldn't be committed
$out = preg_replace ("/^i /", ' $i ', $out);
$out = preg_replace ("/^i /u", ' $i ', $out);
return $out;
}

public function template ($string, $def) {
public function template ($string, $def = NULL) {
$me = $this;

$func = $string;

// deps
if (empty ($def)) {
$func = preg_replace ("/\{\{#([\s\S]+?)\}\}/", "", $func);
$func = preg_replace ("/\{\{#([\s\S]+?)\}\}/u", "", $func);
} else {
$this->def = $def;
$func = $this->resolveDefs ($func);
}

$func = preg_replace ("/'|\\\/", "\\$&", $func);
$func = preg_replace ("/'|\\\/u", "\\\\$0", $func);

// interpolate
$func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/", function ($m) use ($me) {
$func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/u", function ($m) use ($me) {
return "' . " . $me->handleDotNotation ($m[1]) . " . '";
}, $func);
// Conditional
$func = preg_replace_callback ("/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/", function ($m) use ($me) {
$func = preg_replace_callback ("/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/u", function ($m) use ($me) {
$elsecase = $m[1];
$code = $m[2];
if ($elsecase) {
Expand Down Expand Up @@ -91,11 +91,8 @@ public function template ($string, $def) {

$this->functionBody = $func;

return @create_function ('$it', $func);
return eval('return function (' . $this->argName . ') use ($func) {
return eval($func);
};');
}

public function execute ($data) {
return $this->functionCode ($data);
}

}