PHaiku

Widgets

In PHaiku, (almost) all page sections are widgets. The main content is a simple text widget.

Default widgets are set up in the ./config/config.phpfile. To add a widget, add it to the widgets section of that file using this model:

"widgetname" => [
	"handler"=>"handlerFunction",
	"arguments" => ["argument","argument", ...]
],
	
  • widgetname is an alphanumeric string that will appear as a property name that will hold the widget. To output widget content you will use this code in the template: echo $widget->widgetname;
  • handlerFunction is the method name, that will handle the widget, for example textWidget. One method can be used to build many widget's offcourse
  • An array of arguments or NULL if the function take no arguments. Tha arguments will be passed to the method. For example, the textWidget takes only one argument, and that is the filepath to the html file containing the widget content.

Dynamic creation

Widgets defined in the configuration file will be available on all the pages, on all the routes. You may, however, want to add or remove some specific widgets on some routes. To do so, you can use addWidget and removeWidget methods

The addWidget method takes two arguments: the widget name and an array structured the same way than the one in the configuration file (see above).

The removeWidget method takes only one argument: the name of the widget to remove

	addWidget("widgetname",["handler"=>"handlerFunction","arguments"=>["a1","a2",...]]);
	// the above widget is then called and displayed in the template
	echo $widgets->widgetname;
	
	//if we remove the widget
	removeWidget("widgetname");
	//the $widgets->widgetname will now return an empty stringS
	

Existing widget handlers

Some widget methods are provided with PHaiku.

  • the allready mentioned textWidget, that takes a filepath as an argument and displays the file contents
  • The language menu widget, that takes no arguments and displays links to available languages (configurable in the main ./config/config.php file)
  • The menu widget that displays the menu. It takes it's data from the widgets/menu.php file
  • The sample Haiku widgets that displays a random haiku. It takes it's data from the widgets/haiku.php file
  • The newsWidget, that displays latest news. It takes a maximum number of news to be displayed as an argument.

Create your own handlers

Widget handlers are created in the class that extends Phaiku (see the PHaiku folder). While you can directly edit the Haiku.php file, a better idea would be to copy the Haiku.php to something like MyHaiku.php, rename the clas MyHaiku:

namespace PHaiku;

class MyHaiku extends PHaiku {
...
	

and accordingly change the class.name attribute in ./config/config.php

"class.name" => "\PHaiku\MyHaiku",
	

Now you can safely make all the changes you want.

Create a new method in your MyHaiku class:

public function awesomeWidget() {
	// do whatever you need to populate $html with content (like fetching an rss file)
	return $html;
}
	

You can of course add any additional methods that will help your widgets functionality.

Widget views

Sometimes you will want a view file to help you render your data. Your code will be cleaner. Put the view file in your theme folder and call it like this:

$html = $this->app->view->fetch("awesomewidgetview.php");
return $html;	
	

If you have more widget you may want to put all widget views in a separate folder in your theme widgets/awesomewidget.php

Widget data

Put whatever data your widget may need in the widgetsSome widgets subfolder, of your data folder.

If the data is plain html, use html file extension. If your data is more complex (an array for example), then put it in a php file. Sample data for random Linus Torvalds quotes:

<?php
return [
	"Software is like sex: it's better when it's free.",
	"Microsoft isn't evil, they just make really crappy operating systems.",
	"Intelligence is the ability to avoid doing work, yet getting the work done.",
	"Any program is only as good as it is useful."
];
	

I think, that's all you need to know about widgets.

Be creative!

Comments

Feel free to comment or ask questions! Thanks!

comments powered by Disqus

Execution time: 0.0378s Memory usage: 0.67704MB