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
1 changed files with 34 additions and 28 deletions

View File

@ -5,7 +5,7 @@ Widgets are reusable UI components that are able to render themselves and return
## Basic interface
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
{
@ -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:
// in your Controller
public function myControllerAction()
{
$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
a reference to your current view, you can just pass it to the *render()* method:
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:
// in your template
@ -36,37 +37,43 @@ Widgets are normally created in the controller and added to the view:
## 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
Tab bar, you just have to call:
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 Tab bar, you just have to call:
$tabbar = new Tabs();
**Note** : When using an ActionController, there's already an empty tabs object created under $this->view->tabs. This is
done in the preDispatch function
> **Note**: Controllers subclassing `\Icinga\Web\Controller\ActionController` (which all existing controller do so and
> yours should too) have already an empty tabs object created under `$this->view->tabs`. This is done in the
> `preDispatch` function.
### Adding tabs
Afterwards you can add tabs by calling the add($name, $tab) function, whereas $name is the name of your tab and $tab
is either an array with tab parameters or an existing Tab object.
Afterwards you can add tabs by calling the `add($name, $tab)` function, whereas `$name` is the name of your tab and
`$tab` is either an array with tab parameters or an existing Tab object.
// Adding tabs:
$tabbar->add("myTab", array(
"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
));
// Add a tab
$tabbar->add(
'myTab',
array(
'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
Sometimes you want additional actions to be displayed in your tabbar. This can be accomplished with the '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.
Sometimes you want additional actions to be displayed in your tabbar. This can be accomplished with the
`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
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
Tabextension interface that just defines *apply(Tabs $tab)*. A simple example is the DashboardAction Tabextender which
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
just adds a new field to the dropdown list:
class DashboardAction implements Tabextension
@ -79,19 +86,18 @@ just adds a new field to the dropdown list:
$tabs->addAsDropdown(
'dashboard',
array(
'title' => 'Add to Dashboard',
'iconCls' => 'dashboard',
'url' => Url::fromPath('dashboard/addurl'),
'title' => 'Add to Dashboard',
'iconCls' => 'dashboard',
'url' => Url::fromPath('dashboard/addurl'),
'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*
method (which is more fluent):
$tabs->extend(new DashboardAction())
You can now either extend your Tabs object using the DashboardAction's `apply()` method or by calling the Tabs
`extend()` method (which is more fluent):
$tabs->extend(new DashboardAction());