Last active
December 3, 2020 18:59
-
-
Save elliottmangham/7c444faf8d3d769a812ea83d4a646ef3 to your computer and use it in GitHub Desktop.
WordPress (PHP) / Utilities
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
/************************************ | |
* Include component 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_component( $sFilePath, $aScopeVariables = [], $bReturn = false ) { | |
ob_start(); | |
extract( $aScopeVariables, EXTR_OVERWRITE ); | |
$sTemplateSlug = sprintf( '/templates/_components/%s.php', $sFilePath ); | |
$sTemplateLocation = locate_template($sTemplateSlug); | |
if ( ! is_readable( $sTemplateLocation ) ) { | |
throw new \Exception( sprintf( 'Missing component: %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