Widgets: Fix example code in documention not following our Coding Standards

refs #4512
This commit is contained in:
Eric Lippmann 2013-08-16 15:05:28 +02:00
parent 7dc2f78b63
commit 920f3494d8

View File

@ -5,7 +5,7 @@ Widgets are reusable UI components that are able to render themselves and return
## Basic interface ## Basic interface
The interface needed for implementing widgets can be found under library/Icinga/Web/Widget/Widget.php. This is a rather The interface needed for implementing widgets can be found under library/Icinga/Web/Widget/Widget.php. This is a rather
simple interface, only providing a 'render' method that takes a view and returns HTML: simple interface, only providing a `render()` method that takes a view and returns HTML:
interface Widget interface Widget
{ {
@ -19,13 +19,14 @@ When implementing own Widgets you just have to make sure that you provide this r
Widgets are normally created in the controller and added to the view: Widgets are normally created in the controller and added to the view:
// in your Controller // in your Controller
public function myControllerAction() public function myControllerAction()
{ {
$this->view->myWidget = new MyWidget(); $this->view->myWidget = new MyWidget();
} }
The HTML is then rendered in the template using the *render()* method described above. As the '$this' scope in a view is The HTML is then rendered in the template using the `render()` method described above. As the '$this' scope in a view is
a reference to your current view, you can just pass it to the *render()* method: a reference to your current view, you can just pass it to the `render()` method:
// in your template // in your template
@ -36,37 +37,43 @@ Widgets are normally created in the controller and added to the view:
## The 'Tabs' widget ## The 'Tabs' widget
The Tabs (Icinga\Web\Widgets\Tabs) widget handles creation of Tab bars and allows you to create and add single tabs to this view. To create an empty The Tabs `\Icinga\Web\Widgets\Tabs` widget handles creation of Tab bars and allows you to create and add single tabs to
Tab bar, you just have to call: this view. To create an empty Tab bar, you just have to call:
$tabbar = new Tabs(); $tabbar = new Tabs();
**Note** : When using an ActionController, there's already an empty tabs object created under $this->view->tabs. This is > **Note**: Controllers subclassing `\Icinga\Web\Controller\ActionController` (which all existing controller do so and
done in the preDispatch function > yours should too) have already an empty tabs object created under `$this->view->tabs`. This is done in the
> `preDispatch` function.
### Adding tabs ### Adding tabs
Afterwards you can add tabs by calling the add($name, $tab) function, whereas $name is the name of your tab and $tab Afterwards you can add tabs by calling the `add($name, $tab)` function, whereas `$name` is the name of your tab and
is either an array with tab parameters or an existing Tab object. `$tab` is either an array with tab parameters or an existing Tab object.
// Adding tabs: // Add a tab
$tabbar->add("myTab", array(
"title" => "My hosts", // displayed as the tab text $tabbar->add(
"iconCls" => "myicon", // icon-myicon will be used as an icon in a <i> tag 'myTab',
"url" => "/my/url", // the url to use array(
"urlParams" => array("host" => "localhost") // will be used as GET parameter 'title' => 'My hosts', // Displayed as the tab text
)); 'iconCls' => 'myicon', // icon-myicon will be used as an icon in a <i> tag
'url' => '/my/url', // The url to use
'urlParams' => array('host' => 'localhost') // Will be used as GET parameter
)
);
### Adding tabs to the dropdown list ### Adding tabs to the dropdown list
Sometimes you want additional actions to be displayed in your tabbar. This can be accomplished with the 'addAsDropdown' Sometimes you want additional actions to be displayed in your tabbar. This can be accomplished with the
method. This one is similar to the *add* method, but displays your tab in a dropdown list on the right side of the tabbar. `addAsDropdown()` method. This one is similar to the `add()` method, but displays your tab in a dropdown list on the
right side of the tabbar.
## Using tabextensions ## Using tabextensions
Often you find yourself adding the same tabs over and over again. You can write a Tabextension that does this for you Often you find yourself adding the same tabs over and over again. You can write a Tabextension that does this for you
and just apply them on your tabs. Tabextensions are locate the Icinga/Web/Widgets/Tabextension/ and use the simple and just apply them on your tabs. Tabextensions are located at Icinga/Web/Widgets/Tabextension/ and they use the simple
Tabextension interface that just defines *apply(Tabs $tab)*. A simple example is the DashboardAction Tabextender which Tabextension interface that just defines `apply(Tabs $tab)`. A simple example is the DashboardAction Tabextender which
just adds a new field to the dropdown list: just adds a new field to the dropdown list:
class DashboardAction implements Tabextension class DashboardAction implements Tabextension
@ -79,19 +86,18 @@ just adds a new field to the dropdown list:
$tabs->addAsDropdown( $tabs->addAsDropdown(
'dashboard', 'dashboard',
array( array(
'title' => 'Add to Dashboard', 'title' => 'Add to Dashboard',
'iconCls' => 'dashboard', 'iconCls' => 'dashboard',
'url' => Url::fromPath('dashboard/addurl'), 'url' => Url::fromPath('dashboard/addurl'),
'urlParams' => array( 'urlParams' => array(
'url' => Url::fromRequest()->getRelativeUrl() 'url' => Url::fromRequest()->getRelativeUrl()
) )
) )
); );
} }
} }
You can now either extend your Tabs object using the DashboardAction's *apply()* method or by calling the Tabs *extend* You can now either extend your Tabs object using the DashboardAction's `apply()` method or by calling the Tabs
method (which is more fluent): `extend()` method (which is more fluent):
$tabs->extend(new DashboardAction())
$tabs->extend(new DashboardAction());