|
@@ -41,47 +41,54 @@ class PhpTemplate
|
|
|
// PLUGINS
|
|
// PLUGINS
|
|
|
|
|
|
|
|
$template = $this;
|
|
$template = $this;
|
|
|
- $this->addPlugin('layout', function ($param) use ($template) {
|
|
|
|
|
|
|
+ $this->addPlugin('layout', function ($key) use ($template) {
|
|
|
// {layout my_layout_template.tpl.php}
|
|
// {layout my_layout_template.tpl.php}
|
|
|
- $template->layout = $param;
|
|
|
|
|
|
|
+ $template->layout = $key;
|
|
|
return '';
|
|
return '';
|
|
|
});
|
|
});
|
|
|
- $this->addPlugin('\/\/', function ($param) {
|
|
|
|
|
|
|
+ $this->addPlugin('\/\/', function ($key) {
|
|
|
// {// this is a comment}
|
|
// {// this is a comment}
|
|
|
return '';
|
|
return '';
|
|
|
});
|
|
});
|
|
|
- $this->addPlugin('#', function ($param) {
|
|
|
|
|
|
|
+ $this->addPlugin('#', function ($key) {
|
|
|
// {# this is a comment}
|
|
// {# this is a comment}
|
|
|
return '';
|
|
return '';
|
|
|
});
|
|
});
|
|
|
- $this->addPlugin('tr', function ($param) {
|
|
|
|
|
|
|
+ $this->addPlugin('assign', function ($key, $value) use ($template) {
|
|
|
|
|
+ // {assign var_name content of my variable}
|
|
|
|
|
+ $template->assign($key, $value);
|
|
|
|
|
+ return '';
|
|
|
|
|
+ });
|
|
|
|
|
+ $this->addPlugin('tr', function ($key) {
|
|
|
// {tr my text in english} ==> mon texte en francais
|
|
// {tr my text in english} ==> mon texte en francais
|
|
|
- return gettext($param);
|
|
|
|
|
|
|
+ return gettext($key);
|
|
|
});
|
|
});
|
|
|
- $this->addPlugin('include', function ($param) use ($template) {
|
|
|
|
|
|
|
+ $this->addPlugin('include', function ($key) use ($template) {
|
|
|
// {include my_template.tpl.php}
|
|
// {include my_template.tpl.php}
|
|
|
$template = new PhpTemplate($template->templates_dirs, $template->variables, null, $template->templates_dirs);
|
|
$template = new PhpTemplate($template->templates_dirs, $template->variables, null, $template->templates_dirs);
|
|
|
- $templatechild_content = $template->fetch($param);
|
|
|
|
|
|
|
+ $templatechild_content = $template->fetch($key);
|
|
|
return $templatechild_content;
|
|
return $templatechild_content;
|
|
|
});
|
|
});
|
|
|
- $this->addPlugin('routeUrl', function ($param) {
|
|
|
|
|
|
|
+ $this->addPlugin('routeUrl', function ($key, $value=[]) {
|
|
|
// {routeUrl login_page} ===> /login
|
|
// {routeUrl login_page} ===> /login
|
|
|
- $params = explode(' ', $param);
|
|
|
|
|
- $route_name = array_shift($params);
|
|
|
|
|
- $url_args = $params;
|
|
|
|
|
|
|
+ $route_name = $key;
|
|
|
|
|
+ $url_args = explode(' ', $value);
|
|
|
$url = getRouteUrl($route_name, $url_args);
|
|
$url = getRouteUrl($route_name, $url_args);
|
|
|
return $url;
|
|
return $url;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- $this->addPlugin('block', function ($param, $matched_expr, $begin_block_offset_start, &$content) use ($template) {
|
|
|
|
|
|
|
+ $this->addPlugin('block', function ($key, $matched_expr, $begin_block_offset_start, &$content) use ($template) {
|
|
|
// {block block_name}my html content{/block} ==> assign variable $block_name with block content
|
|
// {block block_name}my html content{/block} ==> assign variable $block_name with block content
|
|
|
$begin_block_offset_end = $begin_block_offset_start + strlen($matched_expr);
|
|
$begin_block_offset_end = $begin_block_offset_start + strlen($matched_expr);
|
|
|
|
|
|
|
|
$end_block_offset_start = strpos($content, '{/block}', $begin_block_offset_end);
|
|
$end_block_offset_start = strpos($content, '{/block}', $begin_block_offset_end);
|
|
|
|
|
|
|
|
if ($end_block_offset_start) {
|
|
if ($end_block_offset_start) {
|
|
|
- $block = substr($content, $begin_block_offset_end, $end_block_offset_start - $begin_block_offset_end);
|
|
|
|
|
- $template->assign($param, $block);
|
|
|
|
|
|
|
+ $block = isset($template->variables[$key]) ? $template->variables[$key] : '';
|
|
|
|
|
+
|
|
|
|
|
+ $block = substr($content, $begin_block_offset_end, $end_block_offset_start - $begin_block_offset_end) . $block;
|
|
|
|
|
+
|
|
|
|
|
+ $template->assign($key, $block);
|
|
|
|
|
|
|
|
$end_block_offset_end = $end_block_offset_start + strlen("{/block}");
|
|
$end_block_offset_end = $end_block_offset_start + strlen("{/block}");
|
|
|
$content = substr($content, 0, $begin_block_offset_start) . substr($content, $end_block_offset_end);
|
|
$content = substr($content, 0, $begin_block_offset_start) . substr($content, $end_block_offset_end);
|
|
@@ -135,7 +142,7 @@ class PhpTemplate
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (is_null($tpl_path)) {
|
|
if (is_null($tpl_path)) {
|
|
|
- throw new \Exception("Template not found : " . $tpl, 1);
|
|
|
|
|
|
|
+ throw new \Exception("Template not found : " . $tpl . " (dirs: " . implode(" | ", $tpl_dirs) . ")", 1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//$tpl_vars = array_merge($this->variables, $extra_vars);
|
|
//$tpl_vars = array_merge($this->variables, $extra_vars);
|
|
@@ -162,9 +169,10 @@ class PhpTemplate
|
|
|
foreach ($this->plugins as $prefix => $callback) {
|
|
foreach ($this->plugins as $prefix => $callback) {
|
|
|
|
|
|
|
|
if ($prefix != 'block') {
|
|
if ($prefix != 'block') {
|
|
|
- preg_match_all('/{' . $prefix . ' ([^}]+)}/', $content, $regs, PREG_SET_ORDER);
|
|
|
|
|
|
|
+ preg_match_all('/{' . $prefix . ' ([^} ]+)( ([^}]+))?}/', $content, $regs, PREG_SET_ORDER);
|
|
|
foreach($regs as $reg) {
|
|
foreach($regs as $reg) {
|
|
|
- $replaced = $callback($reg[1], $reg);
|
|
|
|
|
|
|
+ $value = isset($reg[3]) ? $reg[3] : null;
|
|
|
|
|
+ $replaced = $callback($reg[1], $value);
|
|
|
$content = str_replace($reg[0], $replaced, $content);
|
|
$content = str_replace($reg[0], $replaced, $content);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
@@ -187,14 +195,28 @@ class PhpTemplate
|
|
|
|
|
|
|
|
// variables. ex: {$user_name} ==> John
|
|
// variables. ex: {$user_name} ==> John
|
|
|
if (true) {
|
|
if (true) {
|
|
|
- preg_match_all('/{\$([a-zA-Z0-9_\[\]\']+)}/', $content, $regs, PREG_SET_ORDER);
|
|
|
|
|
|
|
+ preg_match_all('/{\$([a-zA-Z0-9._\[\]\']+)}/', $content, $regs, PREG_SET_ORDER);
|
|
|
foreach($regs as $reg) {
|
|
foreach($regs as $reg) {
|
|
|
$var = $reg[1];
|
|
$var = $reg[1];
|
|
|
|
|
+ $var_parts = explode(".", $var);
|
|
|
|
|
|
|
|
- if (isset(${$var})) {
|
|
|
|
|
|
|
+ if (count($var_parts) > 1) {
|
|
|
|
|
+ // $variable.key ==> $variable['key']
|
|
|
|
|
+
|
|
|
|
|
+ $var = ${ array_shift($var_parts) };
|
|
|
|
|
+ foreach ($var_parts as $part) {
|
|
|
|
|
+ $var = $var[ $part ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $replaced = $var;
|
|
|
|
|
+ $content = str_replace($reg[0], $replaced, $content);
|
|
|
|
|
+
|
|
|
|
|
+ } else if (isset(${$var})) {
|
|
|
|
|
+ // $variable
|
|
|
$replaced = ${$var};
|
|
$replaced = ${$var};
|
|
|
$content = str_replace($reg[0], $replaced, $content);
|
|
$content = str_replace($reg[0], $replaced, $content);
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+
|
|
|
// if variable not exists, replace with empty string
|
|
// if variable not exists, replace with empty string
|
|
|
$content = str_replace($reg[0], '', $content);
|
|
$content = str_replace($reg[0], '', $content);
|
|
|
}
|
|
}
|