Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created May 26, 2012 22:33
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 RalfAlbert/2795515 to your computer and use it in GitHub Desktop.
Save RalfAlbert/2795515 to your computer and use it in GitHub Desktop.
Simple-Templates: The Template-Engine
<?php
/**
*
* Abstract class WP Simple Templater creates a copy of the template-class and
* provide the templates to the template-engine
* @author Ralf Albert
*
*/
abstract class WP_Simple_Templater extends Formatter
{
/**
*
* Instance of template-class
* @var object $templates_object
*/
protected $templates_object = NULL;
/**
*
* Constructor
* Creates an instance of the template-class and setup the delimiters for Formatter
* @param WP_Simple_Templates $templates
*/
public function __construct( WP_Simple_Templates $templates ){
$this->templates_object = &$templates;
$this->set_delimiter( '%', '%' );
}
/**
*
* Return a template by given type. Returns an wp-error on failure
* @param string $type
* @return string|bool Return a template-string if it was defined by the template-class. Or false if no such template was defined
*/
protected function get_templates( $templatetype = '', $type = '' ){
// error message
$err_msg = '';
// if the sub-type is empty (e.g the template is a string not an array), use the template-type as sub-type
if( '' == $type )
$type = $templatetype;
// first check if a template-type was set
if( '' == $templatetype )
$err_msg = 'Empty template-type';
// check if a template-class was loaded
elseif( NULL === $this->templates_object )
$err_msg = 'No templates defined in <strong>' . get_class( $this ) . '</strong>';
// we have a template-type and a template-class. get the template(s)
else {
$template = $this->templates_object->get_templates( $templatetype );
// get error-message thrown by template-class
if( is_wp_error( $template ) )
$err_msg = $template->get_error_message();
// check the template.
// convert a string into an array. else check if the requested type is in the template-array
else {
if( ! is_array( $template ) )
$template[$type] = $template;
elseif( ! in_array( $type, array_keys( $template ) ) )
$err_msg = $type . ' is not defined.';
}
}
// if a error occurs, return an error-object
if( '' != $err_msg ){
return new WP_Error(
'template_error',
sprintf(
'<div class="error"><h4>Template Error</h4>%s</div>',
$err_msg
)
);
}
// finally all checks are ok, return the template-array
return $template;
}
}
/**
*
* The concrete class WP Simple HTML insert the data in the templates
* @author Ralf Albert
*
*/
class WP_Simple_HTML extends WP_Simple_Templater
{
/**
*
* Constructor overrides the delimiters in parent class
* @param WP_Simple_Templates $templates
*/
public function __construct( WP_Simple_Templates $templates ){
parent::__construct( $templates );
$this->set_delimiter( '{', '}' );
}
/**
*
* Create a html list (ul, ol or with div-tag)
* @param string $type The type of the list. ul, ol or div
* @param array $data Data to display inside the list
*/
public function get_list( $type = '', $data = array() ){
// get list templates
$templates = $this->get_templates( 'list', $type );
if( is_wp_error( $templates ) )
return $templates->get_error_message();
// create list
$inner = new stdClass();
$values = new stdClass();
foreach( $data as $key => $value ){
$values->key = $key;
$values->item = $value;
$inner->inner .= self::sprintf( $templates[$type]['inner'], $values );
}
return self::sprintf( $templates[$type]['outer'], $inner );
}
/**
*
* Prints a html-list (ul, ol or with div-tag)
* @param string $type The type of the list. ul, ol or div
* @param array $data Data to display inside the list
*/
public function print_list( $type = '', $data = array() ){
if( ! is_array( $data ) )
$data = (array) $data;
echo $this->get_list( $type, $data );
}
/**
*
* Create a H1-headline
* @param string $text
*/
public function get_headline( $text = '' ){
// get headline template
$template = $this->get_templates( 'hone' );
if( is_wp_error( $template ) )
return $template->get_error_message();
$data = new stdClass();
$data->headline = $text;
return self::sprintf( $template, $data );
}
/**
*
* Print a H1-headline
* @param string $text
*/
public function print_headline( $text = '' ){
echo $this->get_headline( $text );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment