Skip to content

Instantly share code, notes, and snippets.

@elliottmangham
Last active December 3, 2020 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliottmangham/3c288cb9477753323b939e821e69e9bf to your computer and use it in GitHub Desktop.
Save elliottmangham/3c288cb9477753323b939e821e69e9bf to your computer and use it in GitHub Desktop.
php / utilities
/************************************
* Include partial file.
* @param string $sFilePath The component to include
* @param array $aScopeVariables An array of variables to make available in the component
* @param boolean $bReturn Return the markup instead off outputting it?
************************************/
function get_partial( $sFilePath, $aScopeVariables = [], $bReturn = false ) {
ob_start();
extract( $aScopeVariables, EXTR_OVERWRITE );
$sTemplateSlug = sprintf( '/templates/_partials/%s.php', $sFilePath );
$sTemplateLocation = locate_template( $sTemplateSlug );
if ( ! is_readable( $sTemplateLocation ) ) {
throw new \Exception( sprintf( 'Missing partial: %s', $sFilePath ) );
}
include $sTemplateLocation;
$sComponent = ob_get_clean();
if ( $bReturn === true ) return $sComponent;
echo $sComponent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment