Created
May 26, 2012 17:54
-
-
Save RalfAlbert/2794775 to your computer and use it in GitHub Desktop.
Simple-Templates: The Template-Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Abstract class Wp Simple Templates define one method to retrive the defined templates | |
* @author Ralf Albert | |
* | |
*/ | |
abstract class WP_Simple_Templates | |
{ | |
/** | |
* | |
* Returns the template if it was defined in the template class. If the requested template was not | |
* defined in the template-class, return a wp-error. | |
* @param string $type | |
* @return array|string|object Array with template-strings or a single template-string or wp-error on failure | |
*/ | |
public function get_templates( $type = '' ){ | |
if( '' == $type ) | |
return NULL; | |
// if the template-class have a method $type, return the template(s). else return a wp-error | |
if( method_exists( $this, $type . '_template' ) ){ | |
$method = $type . '_template'; | |
return $this->$method(); | |
} else { | |
return new WP_error( | |
'template_error', | |
printf( | |
'<h4>Template Error</h4>Template <strong>%s</strong> does not exists in <strong>%s</strong>', | |
$type, | |
get_class( $this ) | |
) | |
); | |
} | |
} | |
/** | |
* | |
* Return an array with all available templates from the template-class | |
*/ | |
public function get_available_templates(){ | |
// get all methods from the abstract class (__CLASS__) and the extended class ($this) | |
$self_methods = get_class_methods( __CLASS__ ); | |
$extended_methods = get_class_methods( $this ); | |
// remove the '_template' extension and return the array with template-names | |
$templates = array(); | |
foreach( array_diff( $extended_methods, $self_methods ) as $template ) | |
array_push( $templates, str_replace( '_template', '', $template ) ); | |
return $templates; | |
} | |
} | |
/** | |
* | |
* Concrete class WP Simple HTML Templates defines the templates | |
* @author Ralf Albert | |
* | |
*/ | |
class WP_Simple_HTML_Templates extends WP_Simple_Templates | |
{ | |
/** | |
* Returns an array with list templates | |
*/ | |
public function list_template(){ | |
return array( | |
'ol' => array( | |
'outer' => "<ol>\n%inner%</ol>\n", | |
'inner' => "<li>%item%</li>\n" | |
), | |
'ul' => array( | |
'outer' => "<ul>\n%inner%</ul>\n", | |
'inner' => "<li>%item%</li>\n" | |
), | |
'div' => array( | |
'outer' => "<div>\n%inner%</div>\n", | |
'inner' => "<p>%item%</p>\n" | |
), | |
); | |
} | |
/** | |
* Returns a headline template with h1-tags | |
*/ | |
public function hone_template(){ | |
return '<h1>%headline%</h1>'; | |
} | |
/** | |
* Returns a paragraph-template with p-tags | |
*/ | |
public function paragraph_template(){ | |
return '<p>%text%</p>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment