Last active
March 22, 2019 11:38
-
-
Save rumur/f9b166dff9fc8b4b5020066bee7fc721 to your computer and use it in GitHub Desktop.
Helper class that works the same as a WP function `get_template_part`, but allows pass variables to that template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Part_Factory | |
{ | |
protected $tpl; | |
protected $name; | |
protected $data = []; | |
/** | |
* Part_Factory constructor. | |
* | |
* @param string $name Dot notation $name e.g. `partials.home.title-item` the `.php` will be added automatically. | |
* @param array $data | |
*/ | |
public function __construct($name, array $data = []) | |
{ | |
$this->data = $data; | |
$this->tpl = $this->nameToPath($this->name = $name); | |
} | |
/** | |
* Displays the content. | |
*/ | |
public function render() | |
{ | |
echo $this->content(); | |
} | |
/** | |
* Adds vars to the tpl. | |
* | |
* @param mixed $var | |
* @param mixed $value | |
* | |
* @return $this | |
*/ | |
public function with($var, $value = null) | |
{ | |
if (is_array($var)) { | |
$this->data = array_merge($this->data, $var); | |
} elseif (is_string($var)) { | |
$this->data[$var] = $value; | |
} | |
return $this; | |
} | |
/** | |
* Makes the HTML from the passed $name and $data | |
* | |
* @return false|string | |
*/ | |
public function content() | |
{ | |
try { | |
if (file_exists($located = locate_template($this->tpl))) { | |
extract($this->data); | |
ob_start(); | |
include $located; | |
return ob_get_clean(); | |
} | |
throw new Exception(sprintf('The <code>%s</code> was not found', $this->tpl)); | |
} catch (Throwable $e) { | |
error_log($e->getMessage()); | |
return defined('WP_DEBUG') && WP_DEBUG ? $e->getMessage() : ''; | |
} | |
} | |
/** | |
* @return false|string | |
*/ | |
public function __toString() | |
{ | |
return $this->content(); | |
} | |
/** | |
* Makes the path from the string divided by "." | |
* | |
* @param string $name The name with dot notation | |
* | |
* @return string Returns the path to the file. | |
*/ | |
protected function nameToPath($name) | |
{ | |
$raw_data = (explode('.', $name)); | |
$file = array_pop($raw_data) . '.php'; | |
$path = trailingslashit(join(DIRECTORY_SEPARATOR, $raw_data)); | |
return $path . $file; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('part')) { | |
/** | |
* Helper to determine the theme partial template and pass the vars into it. | |
* | |
* Example: | |
* - echo part('partials.header-secondary-menu', compact('without_share')); | |
* - echo part('partials.header-secondary-menu')->with(['without_share' => true]); | |
* - echo part('partials.header-secondary-menu')->with('without_share', true); | |
* | |
* @param $name | |
* @param array $data | |
* @return Part_Factory | |
* | |
* @author rumur | |
*/ | |
function part($name, array $data = []) | |
{ | |
return new Part_Factory($name, $data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment