Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active April 14, 2019 23:48
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 mishterk/f8394fb2b48b18ab0fbd3d7a11c1d703 to your computer and use it in GitHub Desktop.
Save mishterk/f8394fb2b48b18ab0fbd3d7a11c1d703 to your computer and use it in GitHub Desktop.
A basic, static view handler for WordPress

Basic Usage

// set up
View::$view_dir = '/some/path';

// echos the view
View::render('relative/template', [
  'var1' => 'data',
  'var2' => 'more data'
]);

// or, assign to variable
$markup = View::prepare('relative/template', [
  'var1' => 'data',
  'var2' => 'more data'
]);

Support Overrides

If a theme view dir is specified, the class will attempt to load any templates marked as overridable from the theme dir. If not found, the template in the base dir will be used instead.

// set up
View::$view_dir = '/some/path';
View::$theme_view_dir = get_stylesheet_dir() . '/templates';

// mark specific templates as overridable
View::$overridable_templates = [
  'relative/template.php', // need the file extension
];

// mark entire directories as overridable
View::$overridable_template_dirs = [
  'relative',
];

// echos the view
View::render('relative/template', [
  'var1' => 'data',
  'var2' => 'more data'
]);
<?php
class View {
/**
* @var string The view directory inside the plugin
*/
public static $view_dir = '';
/**
* @var string The alternate view directory we check for overridable template resolution.
*/
public static $theme_view_dir = '';
/**
* @var array An array of template names that are checked for override in the theme. These include the template paths
* relative to the view directory.
*/
public static $overridable_templates = [];
/**
* @var array An array of template directory names that are checked for override in the theme. These must include
* the path relative to the view directory.
*/
public static $overridable_template_dirs = [];
/**
* Render View Template With Data
*
* Locates a view template and includes it within the same scope as a data object/array. This makes it possible to
* access raw data in the template.
*
* Note: Any data passed into this function will be casted as an array and then as an object. The final data available
* within a template is in the form of an object with the variable name $data.
*
* e.g.
*
* array('name' => 'Bob', 'age' => 42)
*
* Will be converted to an object to be used as;
*
* $data->name
* $data->age
*
* @param string|null $name A named variation for the template. This is in the form {$name}.php. Can include directories, where necessary.
* @param object|array $data An associative array or object to use inside the template.
* @param string $suffix The file suffix.
*
* @return string
*/
public static function prepare( $name, $data = [], $suffix = '.php' ) {
$markup = '';
$path = self::get_full_path( $name . $suffix );
if ( $t = self::view_template_exists( $path ) ) {
$data = self::prepare_data( $data );
$markup = self::enclose_vars_with_template( $path, $data );
}
return $markup;
}
public static function enclose_vars_with_template( $path, $data ) {
extract( $data );
ob_start();
include $path;
$markup = ob_get_clean();
return $markup;
}
/**
* Use this to echo out templates
*
* @param $name
* @param array $data
* @param string $suffix
*/
public static function render( $name, $data = [], $suffix = '.php' ) {
echo self::prepare( $name, $data, $suffix );
}
/**
* Casts data to an array for exraction before template inclusion
*
* @param $data
*
* @return array
*/
private static function prepare_data( $data ) {
// if data is not already an object, cast as object
if ( ! is_array( $data ) ) {
$data = (array) $data;
}
return $data;
}
/**
* Making sure the template exists
*
* @param $name
*
* @return bool
*/
private static function view_template_exists( $name ) {
return file_exists( $name );
}
/**
* Pieces together the full path to the file
*
* @param $name
*
* @return string
*/
private static function get_full_path( $name ) {
// attempt to resolve template in override directory (wp theme)
if ( self::template_is_overridable( $name ) ) {
$override_path = trailingslashit( self::$theme_view_dir ) . ltrim( $name, '/' );
if ( self::view_template_exists( $override_path ) ) {
return $override_path;
}
}
return trailingslashit( self::$view_dir ) . ltrim( $name, '/' );
}
/**
* @param $name
*
* @return bool
*/
private static function template_is_overridable( $name ) {
// check explicitly declared templates
if ( in_array( $name, self::$overridable_templates ) ) {
return true;
}
// check if name starts with a declared directory
foreach ( self::$overridable_template_dirs as $dir ) {
if ( strpos( $name, $dir ) === 0 ) {
return true;
}
}
return false;
}
public static function todo( $message ) {
ob_start();
?>
<span style="color:red; display: inline-block; clear:both;">TODO: <?php echo $message; ?></span>
<?php
return ob_get_clean();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment