|
@@ -10,10 +10,17 @@
|
|
|
private static $templates = [];
|
|
|
private $template;
|
|
|
private $name;
|
|
|
- public function __construct(string $name, callable $template){
|
|
|
+ public function __construct(string $name, string $template, bool $is_file = false){
|
|
|
if(isset(static::$templates[$name])){
|
|
|
throw new Exception("Template {$name} already exists");
|
|
|
}
|
|
|
+ if($is_file){
|
|
|
+ $path = realpath($template);
|
|
|
+ if(!file_exists($path)){
|
|
|
+ throw new Exception("Template file {$template} doesn't exist");
|
|
|
+ }
|
|
|
+ $template = file_get_contents($path);
|
|
|
+ }
|
|
|
$this->template = $template;
|
|
|
$this->name = $name;
|
|
|
static::$templates[$name] = $this;
|
|
@@ -23,10 +30,7 @@
|
|
|
if($this->fire('before', $data) === false){
|
|
|
throw new Exception("Render on template {$this->name} cancelled. Before.");
|
|
|
}
|
|
|
- ob_start();
|
|
|
- $output = \Closure::FromCallable($this->template)->call($data);
|
|
|
- $output .= ob_get_contents();
|
|
|
- ob_end_clean();
|
|
|
+ $output = static::parse($this->template, $data);
|
|
|
if($this->fire('after', $output) === false){
|
|
|
throw new Exception("Render on template {$this->name} cancelled. After");
|
|
|
}
|
|
@@ -39,5 +43,16 @@
|
|
|
}
|
|
|
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]);
|
|
|
+ }
|
|
|
+ ], $template);
|
|
|
+ return $output;
|
|
|
+ }
|
|
|
}
|
|
|
?>
|