Skip to content

Instantly share code, notes, and snippets.

@mishterk mishterk/class-view.php
Last active Oct 23, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.