Skip to content

Instantly share code, notes, and snippets.

@mudrd8mz
Created October 14, 2011 13:32
Show Gist options
  • Save mudrd8mz/1287112 to your computer and use it in GitHub Desktop.
Save mudrd8mz/1287112 to your computer and use it in GitHub Desktop.
Extending the concept of renderable components so that the rendering methods can be organized into hierarchies
/**
* Tries to render the given widget or delegates the task to the parent renderer
*
* In order to be able to render the given widget, this renderer must implement
* a protected method called render_widgetname() where widgetname is the name of
* the widget's class or some of its parents. The rendering method is supposed
* to accept the single parameter - the renderable widget itself.
*
* @param renderable $widget some object implementing the renderable interface
* @return string HTML code representing the widget
*/
public function render(renderable $widget) {
// populate the list of class names to try
$tryclasses = array(get_class($widget));
foreach (class_parents($widget) as $parent) {
if (in_array('renderable', class_implements($parent))) {
$tryclasses[] = $parent;
}
}
foreach ($tryclasses as $classname) {
$rendermethod = 'render_'.$classname;
if (method_exists($this, $rendermethod)) {
return $this->$rendermethod($widget);
}
}
// pass to the parent renderer if none suitable method was found here
return $this->output->render($widget);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment