Skip to content

Instantly share code, notes, and snippets.

@Mamaduka
Created January 23, 2021 06:29
Show Gist options
  • Save Mamaduka/a27799331cd1e748512c4b98f9657b77 to your computer and use it in GitHub Desktop.
Save Mamaduka/a27799331cd1e748512c4b98f9657b77 to your computer and use it in GitHub Desktop.
View helper function
<?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