Widgets

Seotoaster’s plugins can bring their own widgets into the system. It makes shortcodes look nicer and allows to move a part of routine stuff from the main plugin’s class to more suitable location. Let’s try to create a widget for our todo plugin.

In the plugin we have 2 option makers for the form and the list options. Let’s do more elegant way and create a widget called Todo (yes, the same name with the plugin) and teach it to handle our options. All plugin’s widgets should stay in the plugin_directory/system/app/Widgets folder. Each widget should have its own directory. So, we will create a directory called Todo and create a class called Todo.php in it. Check it out

todo

config

system

app

Api

MagicSpaces

Todo

Widgets

Todo

Todo.php

All widgets should extend Widgets_Abstract class and implements protected _load method. Initially your widget class should look similar to the example

_load method should return content that will be place instead of the widget shortcode. Widgets are able to work with views. Let’s add a directory called views in our widget directory and override protected _init() method of the widget to initialize the view property

The next step is to change the short codes that you put in the content to the new ones. Go and change {%%plugin:todo:form} to {%%todo:form} and {%%plugin:todo:list} to {%%todo:list} Those new shortcodes will tell the Seotoaster to work with your plugin’s widget. Lets move our form.todo.phtml and list.todo.phtml to the widget’s view directory. Similar to the plugin, everything that goes after the : in the widget colled “options”. All options, passed to the widget, could be found in the $this->_options array. Also a lot of usefull data could be found in the $_toasterOptions array. (see the Widgets_Abstract class for more information such as automated widget caching, etc...) Okay, we are almost there, lets modify our widget’s code to handle the options. Checkout an example below

This is the one way to organize options handling. At first we generate a “renderer” name - name of the protected method of our widget that will be responsible for the option, and, if this method exists we try to execute it. That easy! Finally go to the Todo plugin’s main class and remove _makeOptionForm and _makeOptionList methods. We do not need them anymore. That’s it! We moved neccessary code to the widget! Let’s move on and see what else can we do