Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active May 14, 2021 06:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/bdab218e813b9d73bcfa86e51a0f366d to your computer and use it in GitHub Desktop.
Save mishterk/bdab218e813b9d73bcfa86e51a0f366d to your computer and use it in GitHub Desktop.
Using generic views inside WordPress for a more flexible UI system. For more info see https://philkurth.com.au/articles/use-generic-views-with-data-arrays-for-a-flexible-context-independent-ui-system/
<?php
class View {
public static $view_dir = '';
/**
* 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 );
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 object for use int the template
*
* @param $data
*
* @return object
*/
private static function prepare_data( $data ) {
// if data is not already an object, cast as object
if ( ! is_object( $data ) ) {
$data = (object) (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 ) {
return trailingslashit( self::$view_dir ) . ltrim( $name, '/' );
}
}
<?php
include 'inc/View.php';
View::$view_dir = get_stylesheet_directory() . '/templates';
<?php
get_header();
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
View::render( 'post-single', [
'post_title' => get_the_title(),
'content' => get_the_content(),
'author' => get_the_author(),
] );
}
} else {
?>
<p>No post found.</p>
<?php
}
get_footer();
<?php
$data = isset( $data ) ? $data : new stdClass();
$post_title = isset( $data->post_title ) ? $data->post_title : "";
$author = isset( $data->author ) ? $data->author : "";
$content = isset( $data->content ) ? $data->content : "";
?>
<div class="PostSingle">
<?php if ( $post_title ): ?>
<h1 class="PostSingle__title"><?= $post_title ?></h1>
<?php endif; ?>
<?php if ( $author ): ?>
<div class="PostSingle__author"><?= $author ?></div>
<?php endif; ?>
<?php if ( $content ): ?>
<div class="PostSingle__content"><?= $content ?></div>
<?php endif; ?>
</div>
<?php
View::render( 'post-single', [
'post_title' => get_the_title(),
'content' => get_the_content(),
'author' => get_the_author(),
] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment