Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created May 26, 2012 22:37
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/2795518 to your computer and use it in GitHub Desktop.
Save RalfAlbert/2795518 to your computer and use it in GitHub Desktop.
Simple-Templates: The Demo
<?php
class Template_Engine_Test
{
/**
*
* Container for the different template-engines
* @var object $template_engine
*/
public static $template_engine;
/**
*
* Initial some vars
*/
public function __construct(){
// initialize the template-engine to avoid strict errors
self::$template_engine = new stdClass();
}
/**
*
* Caching for the template-classes
* Create a WP-Error if the template-engine could not be initialize
* @param string $template_engine The requested template-engine
* @return object The requested template-engine if available
*/
protected function get_template_engine( $template_engine ){
$template_engine = strtolower( $template_engine );
// available template-engines and the template-classes
$available_template_engines = array(
'html' => array( 'WP_Simple_HTML', 'WP_Simple_HTML_Templates' )
);
$available_template_engines = apply_filters( 'available_template_engines', $available_template_engines );
// check if the requested template-engine is available. if not, try to create it
if( ! isset( self::$template_engine->$template_engine ) ){
if( ! isset( $available_template_engines[$template_engine] ) ){
return new WP_Error( 'template_engine', 'Unknown template-engine <strong>' . $template_engine . '</strong>' );
} else {
$tmpl_ng = &$available_template_engines[$template_engine];
$class = &$tmpl_ng[0];
$template = &$tmpl_ng[1];
self::$template_engine->$template_engine = new $class( new $template );
}
}
return self::$template_engine->$template_engine;
}
/**
*
* The output
*/
public function output( $stream ){
$output = $this->get_template_engine( $stream );
if( is_wp_error( $output ) )
die( $output->get_error_message() );
$output->print_headline( strtoupper( $stream ) );
$list = $output->get_list( 'ol', array( 'eins', 'zwei', 'drei' ) );
echo $list;
}
/**
*
* Setup html-mode
*/
public function set_html(){
// html is build in
}
/**
*
* Setup xml-mode
*/
public function set_xml(){
add_filter( 'available_template_engines', array( &$this, 'add_xml' ), 1, 0 );
}
/**
*
* Add the XML-Template-Engine
*/
public function add_xml(){
return array(
'xml' => array( 'WP_Simple_HTML', 'WP_Simple_HTML_Templates' ),
);
}
public function set_feed(){
add_filter( 'available_template_engines', array( &$this, 'add_feed' ), 1,1 );
}
public function add_feed( $template_engines ){
$feed_engines = array(
'rss' => array( 'WP_Simple_HTML', 'WP_Simple_HTML_Templates' ),
'atom' => array( 'WP_Simple_HTML', 'WP_Simple_HTML_Templates' ),
);
return array_merge( $template_engines, $feed_engines );
}
}
$html = new Template_Engine_Test();
$html->set_html();
$html->output( 'html' );
$xml = new Template_Engine_Test();
$xml->set_xml();
$xml->output( 'xml' );
$feed = new Template_Engine_Test();
$feed->set_feed();
$feed->output( 'rss' );
$feed->output( 'atom' );
$bar = new Template_Engine_Test();
$bar->output( 'wallawalla' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment