Created
January 23, 2021 06:29
-
-
Save Mamaduka/a27799331cd1e748512c4b98f9657b77 to your computer and use it in GitHub Desktop.
View helper function
This file contains hidden or 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 | |
namespace Mamaduka\ProjectName\Helpers; | |
use const Mamaduka\ProjectName\PLUGIN_DIR; | |
/** | |
* Render a view. | |
* | |
* @param string $name Name of the view. | |
* @param array $data Data passed to the view. | |
* @return void | |
* | |
* @throws \Exception | |
*/ | |
function view( $name = '', array $data = [] ) { | |
$path = PLUGIN_DIR . '/views/' . $name; | |
if ( ! file_exists( $path ) ) { | |
throw new \Exception( sprintf( 'Unable to locate view file: %s', $path ) ); | |
} | |
extract( $data, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract | |
require $path; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment