This tutorial will walk you through the steps for creating an addon that will replace the {body_html} of a template with the text Hello World. In other words, it allows you to make your addon display it's own page. It works very similarly to addon tags, the difference is that this is for working with the main part of the page. With a little modification, you can even make your addon create the whole page, instead of relying on an overall page template.
If you need your addon to display it's own page, use this tutorial as a starting point.
Requires Geo 4.0.0
The following is not possible in Geo 3.1 and earlier, because the functionality for an addon to display it's own page was added to the addon system in 4.0.
<?php //file /addons/hello_world/info.php class addon_hello_world_info { public $name = 'hello_world'; public $version = '1.0.0'; public $title = 'Hello World'; public $author = "Hello World, Inc."; public $description = 'Displays "Hello World" page.'; public $auth_tag = 'my_addons'; public $pages = array ( 'hello_world_page' ); }
<
<?php //file: /addons/hello_world/pages.php class addon_hello_world_pages { public function hello_world_page () { //Note: The method name matches the page name used in the $pages var in info.php return "Hello World!"; } }
<
This isn't the limit of what addons can do, to see everything addons can do get the example addon (should be a free addon in the client area). It further demonstrates the use of addon pages, along with all of the other current abilities of addons.
You are not limited to returning the text you want to be displayed on the page, below are some alternate things you can do.
At the time the method is called in the pages.php file, nothing is displayed on the page yet, even if cache is turned on. This means, if you wanted to you could display the entire page yourself, by doing something like this for your pages.php file:
<?php //file: /addons/hello_world/pages.php //Alternate, to display the entire page class addon_hello_world_pages { public function hello_world_page () { //Note: The method name matches the page name used in the $pages var in info.php ?> <html> <head> <title>Hello World!</title> </head> <body><h1>Hello World!</h1> <p>The addon is displaying the entire page now, no built in templates involved!</p> </body> </html> <?php //now since we just displayed the entire page, prevent the main page from displaying: geoView::getInstance()->setRendered(true); //The return value is not used when we make the preceding call. return ""; } }
When you use this method, you do not need to worry about setting the template for the page as described in step 6 of the original instructions, since this method uses no templates.
Even though this is listed as an "alternate" display method, we actually recommend doing this for anything more simple than displaying something like "Hello World!" on the page. We list this as an "alternate" display method, even though it is the one we recommend, in order to keep the main instructions as simple and easy to follow as possible.
For help creating Smarty templates, see the official Smarty.net website and their documentation. Have you read through the Smarty site and are ready to start using Smarty templates for your addon? Good! Then follow the instructions below.
<?php //file: /addons/hello_world/pages.php //extend our info class, so we can use $this-> to access vars in it class addon_hello_world_pages extends addon_hello_world_info { public function hello_world_page () { //Note: The method name matches the page name used in the $pages var in info.php //First, get instance of the view class $view = geoView::getInstance(); //use the template file addons/hello_world/templates/hello_world.tpl: $view->setBodyTpl('hello_world.tpl',$this->name); //example of assigning a template variable that is local in scope to the template //file. see that function for more details. $view->setBodyVar('display_hello_world',true); return "";//The templates are doing the work, so don't need to return anything. } }
<