|
@@ -8,6 +8,16 @@
|
|
|
class Template {
|
|
|
use Events;
|
|
|
private static $templates = [];
|
|
|
+ private static $regex = [
|
|
|
+ 'match'=>'/\{([^#\/][^}\n]+?)\}/i',
|
|
|
+ 'each'=>'/\{#each ([^}]*)\}([\S\s]*)\{\/each\}/i',
|
|
|
+ 'exist'=>'/\{#exist ([^}]*)\}([\S\s]*)\{\/exist\}/i',
|
|
|
+ 'existelse'=>'/\{#exist ([^}]*)\}([\S\s]*)\{#else\}([\S\s]*)\{\/exist\}/i',
|
|
|
+ 'ignore'=>'/\{#ignore\}([\S\s]*)\{\/ignore\}/i',
|
|
|
+ 'ignored'=>'/\{#ignored (\d+?)\}/i',
|
|
|
+ 'gettext'=>"/{_([^,}]+)(?:, ?([^},]+))*\}/i",
|
|
|
+ 'gettext_string'=>'/^([\'"])(.+)\1$/i'
|
|
|
+ ];
|
|
|
private $template;
|
|
|
private $name;
|
|
|
public function __construct(string $name, string $template, bool $is_file = false){
|
|
@@ -44,14 +54,53 @@
|
|
|
return $template->run($data);
|
|
|
}
|
|
|
public static function parse(string $template, $data){
|
|
|
- $output = preg_replace_callback_array([
|
|
|
- '/\{([^#\/][^}\n]+?)\}/i'=> function($match) use($data){
|
|
|
- return $data[$match[1]] ?? '';
|
|
|
- },
|
|
|
- "/_\(['\"]?([^']+)['\"]?\)/i"=> function($match){
|
|
|
- return _($match[1]);
|
|
|
+ $ignored = [];
|
|
|
+ // Handle {#ignore code}
|
|
|
+ $output = preg_replace_callback(static::$regex['ignore'], function($matches) use($ignored){
|
|
|
+ $ignored[] = $matches[1];
|
|
|
+ return '{#ignored '.count($ignored).'}';
|
|
|
+ }, $template);
|
|
|
+ // Handle {#each name}{/each}
|
|
|
+ $output = preg_replace_callback(static::$regex['each'], function($matches) use($data){
|
|
|
+ $output = '';
|
|
|
+ if(isset($data[$matches[1]])){
|
|
|
+ foreach($data[$matches[1]] as $item){
|
|
|
+ $output = static::parse($matches[2], $item);
|
|
|
+ }
|
|
|
}
|
|
|
- ], $template);
|
|
|
+ return $output;
|
|
|
+ }, $output);
|
|
|
+ // Handle {#exist name}{#else}{/exist}
|
|
|
+ $output = preg_replace_callback(static::$regex['existelse'], function($matches) use($data){
|
|
|
+ if(isset($data[$matches[1]])){
|
|
|
+ $output = static::parse($matches[2], $data);
|
|
|
+ }else{
|
|
|
+ $output = static::parse($matches[3], $data);
|
|
|
+ }
|
|
|
+ return $output;
|
|
|
+ }, $output);
|
|
|
+ // Handle {#exist name}{/exist}
|
|
|
+ $output = preg_replace_callback(static::$regex['exist'], function($matches) use($data){
|
|
|
+ if(isset($data[$matches[1]])){
|
|
|
+ return static::parse($data[$matches[2]], $data);
|
|
|
+ }
|
|
|
+ return '';
|
|
|
+ }, $output);
|
|
|
+ // Handle {gettext}
|
|
|
+ $output = preg_replace_callback(static::$regex['gettext'], function($matches) use($data){
|
|
|
+ $args = array_map(function($item) use($data){
|
|
|
+ if(preg_match(static::$regex['gettext_string'], $item)){
|
|
|
+ return preg_replace(static::$regex['gettext_string'], '\2', $item);
|
|
|
+ }else{
|
|
|
+ return $data[$item] ?? '';
|
|
|
+ }
|
|
|
+ }, array_slice($matches, 1));
|
|
|
+ return _(sprintf(...$args));
|
|
|
+ }, $output);
|
|
|
+ // Handle {name}
|
|
|
+ $output = preg_replace_callback(static::$regex['match'], function($matches) use($data){
|
|
|
+ return $data[$matches[1]] ?? '';
|
|
|
+ }, $output);
|
|
|
return $output;
|
|
|
}
|
|
|
}
|