Customizing Theme Behavior

From Habari Project

Jump to: navigation, search

In addition to the ability of using custom templates to display output for particular requests, Habari allows customization of the code that controls which templates are called for display and what values are passed to them.

To accomplish this, Habari attempts to load a theme.php file located in the theme directory. If this file is present, it is included in the code before the theme is rendered for any incoming request.

To override the core behavior for themes (defined in /system/classes/theme.php), define a new class structure in the theme's theme.php that extends the core Theme class.

Consider this custom class:

/**
 * A custom theme class
 * Custom themes are not required for themes, but they are handy in letting you 
 * define your own output data and possibly even additional, non-standard templates.
 **/   
class CustomTheme extends Theme
{
	/**
	 * Override Theme::act_display_posts() to provide alternate post display behavior
	 **/	 	 	
	public function act_display_posts()
	{
		parent::act_display_posts();
	}
 
	/**
	 * Add Theme::act_display_mine() to provide new, custom display behavior
	 **/	 	 	
	public function act_display_mine()
	{
		parent::act_display_posts();
	}
 
}


After overrided the core Theme class, you need change your theme.xml to tell habari which theme class to using.

<?xml version="1.0" encoding="UTF-8" ?>
<pluggable type="theme">
        <name>Your Theme Name</name>
        <class>YOUR_CLASS_NAME</class>
        <author>YOUR_NAME</author>
        ....
</pluggable>


This class defines two custom functions for output. Both of these functions would be executed based on the action field value set for the rewrite_rules record that matches an incoming URL request.

If you decide to override a class method, and you want to retain the parent method's functionality, then you must call the parent method explicitly.

If, for example, you did not want to execute the default behavior, having Habari execute that outside of the regular class calling mechanism would make it so that you couldn't prevent the default behavior.

An alternative that might work for you is to use the add_template_vars plugin action to add your variables, rather than implementing add_template_vars() in your theme class.

So instead of:

 function add_template_vars() {
   $this->foo = 'bar';
 }

You'd use:

 function action_add_template_vars($theme) {
   $theme->foo = 'bar';
 }
Personal tools
This is a cached copy of the requested page, and may not be up to date.

Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.

(Cannot contact the database server: Access denied for user 'habari'@'localhost' to database 'habari_wiki' (localhost))


You can try searching via Google in the meantime.
Note that their indexes of our content may be out of date.