controller
5 years ago
core
5 years ago
Ga_Admin.php
4 years ago
Ga_Autoloader.php
4 years ago
Ga_Frontend.php
5 years ago
Ga_Helper.php
4 years ago
Ga_Hook.php
4 years ago
Ga_Notice.php
6 years ago
Ga_Sharethis.php
6 years ago
Ga_Stats.php
4 years ago
Ga_Template.php
4 years ago
Ga_Template.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Ga_Template |
| 5 | */ |
| 6 | class Ga_Template { |
| 7 | /** |
| 8 | * @var array Array of template properties. |
| 9 | */ |
| 10 | protected $props; |
| 11 | |
| 12 | /** |
| 13 | * @var string Relative path in view/ folder. |
| 14 | */ |
| 15 | protected $path; |
| 16 | |
| 17 | /** |
| 18 | * Ga_Template constructor. |
| 19 | * |
| 20 | * @param string $path Relative path in view/ folder. |
| 21 | * @param array $props Array of props to be passed to the template. |
| 22 | */ |
| 23 | public function __construct( $path, $props = [] ) { |
| 24 | $this->path = $path; |
| 25 | $this->props = $props; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Include rendered template inline. |
| 30 | * |
| 31 | * @param string $path Relative path in view/ folder. |
| 32 | * @param array $props Array of props to be passed to the template. |
| 33 | */ |
| 34 | public static function load( $path, $props = [] ) { |
| 35 | ( new static( $path, $props ) )->includeTemplate(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get rendered template. |
| 40 | * |
| 41 | * @param string $path Relative path in view/ folder. |
| 42 | * @param array $props Array of props to be passed to the template. |
| 43 | * |
| 44 | * @return string Rendered template. |
| 45 | */ |
| 46 | public static function render( $path, $props = [] ) { |
| 47 | return ( new static( $path, $props ) )->renderTemplate(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Include template. |
| 52 | */ |
| 53 | public function includeTemplate() { |
| 54 | $template_path = GA_PLUGIN_DIR . '/view/' . $this->path . '.php'; |
| 55 | |
| 56 | if ( is_readable( $template_path ) ) { |
| 57 | load_template( $template_path, false, $this->props ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get rendered template. |
| 63 | * |
| 64 | * @return string |
| 65 | */ |
| 66 | public function renderTemplate() { |
| 67 | ob_start(); |
| 68 | $this->includeTemplate(); |
| 69 | $render = ob_get_contents(); |
| 70 | |
| 71 | return $render ?: ''; |
| 72 | } |
| 73 | } |