| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FL\Assistant\System; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class View |
| 8 |
* @package FL\Assistant\Util |
| 9 |
* |
| 10 |
* @phpcs:disable WordPress.PHP.DontExtract.extract_extract |
| 11 |
*/ |
| 12 |
class View { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var string path to template dir |
| 16 |
*/ |
| 17 |
protected $template_dir; |
| 18 |
|
| 19 |
public function __construct( $template_dir ) { |
| 20 |
$this->template_dir = $template_dir; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $template_dir |
| 25 |
* |
| 26 |
* @return $this |
| 27 |
*/ |
| 28 |
public function set_template_dir( $template_dir ) { |
| 29 |
$this->template_dir = $template_dir; |
| 30 |
|
| 31 |
return $this; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @return string path |
| 36 |
*/ |
| 37 |
public function get_template_dir() { |
| 38 |
return $this->template_dir; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Render template to string |
| 44 |
* |
| 45 |
* @param $template_name |
| 46 |
* @param array $data |
| 47 |
* |
| 48 |
* @return false|string |
| 49 |
*/ |
| 50 |
public function render_to_string( $template_name, array $data = [] ) { |
| 51 |
$path = sprintf( '%s/%s.php', $this->template_dir, $template_name ); |
| 52 |
|
| 53 |
ob_start(); |
| 54 |
extract( $data ); |
| 55 |
include( $path ); |
| 56 |
$output = ob_get_clean(); |
| 57 |
|
| 58 |
return $output; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Render template to echo |
| 63 |
* |
| 64 |
* @param $template_name |
| 65 |
* @param array $data |
| 66 |
*/ |
| 67 |
public function render( $template_name, array $data = [] ) { |
| 68 |
echo $this->render_to_string( $template_name, $data ); |
| 69 |
} |
| 70 |
} |
| 71 |
|