Skip to content

Instantly share code, notes, and snippets.

@rossberenson
Forked from jo-flynn/functions.php
Last active March 26, 2020 18:47
Show Gist options
  • Save rossberenson/3d9aee7e21c5666a38e8fafbcffc9bbf to your computer and use it in GitHub Desktop.
Save rossberenson/3d9aee7e21c5666a38e8fafbcffc9bbf to your computer and use it in GitHub Desktop.
Get Template Part With Arguments ( Forked incase if author removes it )
<?php
/**
* Load a template part & pass in variables declared in caller scope. Optionally return as a string.
* @param string $path path to template file, minus .php (eg. `content-page`, `partial/folder/template-name`)
* @param array $args map of variables to load into scope
* @param bool $echo echo or return rendered template
* @return null or rendered template string
*/
function rb_get_template_part($path, $args = [], $echo = true) {
if (!empty($args)) {
extract($args);
}
if ($echo) {
include(locate_template($path . '.php'));
return;
}
ob_start();
include(locate_template($path . '.php'));
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment