Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit a729cf6

Browse files
committed
Merge pull request #26 from GeeH/hotfix/added-docs
Added base documentation
2 parents cafc863 + 1d538db commit a729cf6

25 files changed

+3639
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# Advanced usage of helpers
2+
3+
## Registering Helpers
4+
5+
`Zend\View\Renderer\PhpRenderer` composes a *plugin manager* for managing helpers, specifically an
6+
instance of `Zend\View\HelperPluginManager`, which extends
7+
`Zend\ServiceManager\AbstractPluginManager`, and this extends `Zend\ServiceManager\ServiceManager`.
8+
As you can see, the *HelperPluginManager* is a specialized service manager, so you can register a
9+
helper/plugin like any other service (see the Service Manager documentation
10+
<zend.service-manager.intro> for more information).
11+
12+
Programmatically, this is done as follows:
13+
14+
```php
15+
// $view is an instance of PhpRenderer
16+
$pluginManager = $view->getHelperPluginManager();
17+
18+
// Register as a invokable class:
19+
$pluginManager->setInvokableClass('lowercase', 'MyModule\View\Helper\LowerCase');
20+
21+
// Register as a factory:
22+
$pluginManager->setFactory('lowercase', function ($pluginManager) {
23+
$lowercaseHelper = new MyModule\View\Helper\LowerCase;
24+
25+
// ...do some configuration or dependency injection...
26+
27+
return $lowercaseHelper;
28+
});
29+
```
30+
31+
Within an MVC application, you will typically simply pass a map of plugins to the class via your
32+
configuration.
33+
34+
```php
35+
// From within a configuration file
36+
return array(
37+
'view_helpers' => array(
38+
'invokables' => array(
39+
'lowercase' => 'MyModule\View\Helper\LowerCase',
40+
'uppercase' => 'MyModule\View\Helper\UpperCase',
41+
),
42+
),
43+
);
44+
```
45+
46+
If your module class implements `Zend\ModuleManager\Feature\ViewHelperProviderInterface`, or just
47+
the method `getViewHelperConfig()`, you could do the following (it's the same as the previous
48+
example).
49+
50+
```php
51+
namespace MyModule;
52+
53+
class Module
54+
{
55+
public function getAutoloaderConfig(){ /*common code*/ }
56+
public function getConfig(){ /*common code*/ }
57+
58+
public function getViewHelperConfig()
59+
{
60+
return array(
61+
'invokables' => array(
62+
'lowercase' => 'MyModule\View\Helper\LowerCase',
63+
'uppercase' => 'MyModule\View\Helper\UpperCase',
64+
),
65+
);
66+
}
67+
}
68+
```
69+
70+
The two latter examples can be done in each module that needs to register helpers with the
71+
`PhpRenderer`; however, be aware that another module can register helpers with the same name, so
72+
order of modules can impact which helper class will actually be registered!
73+
74+
## Writing Custom Helpers
75+
76+
Writing custom helpers is easy. We recommend extending `Zend\View\Helper\AbstractHelper`, but at the
77+
minimum, you need only implement the `Zend\View\Helper\HelperInterface` interface:
78+
79+
```php
80+
namespace Zend\View\Helper;
81+
82+
use Zend\View\Renderer\RendererInterface as Renderer;
83+
84+
interface HelperInterface
85+
{
86+
/**
87+
* Set the View object
88+
*
89+
* @param Renderer $view
90+
* @return HelperInterface
91+
*/
92+
public function setView(Renderer $view);
93+
94+
/**
95+
* Get the View object
96+
*
97+
* @return Renderer
98+
*/
99+
public function getView();
100+
}
101+
```
102+
103+
If you want your helper to be capable of being invoked as if it were a method call of the
104+
`PhpRenderer`, you should also implement an `__invoke()` method within your helper.
105+
106+
As previously noted, we recommend extending `Zend\View\Helper\AbstractHelper`, as it implements the
107+
methods defined in `HelperInterface`, giving you a headstart in your development.
108+
109+
Once you have defined your helper class, make sure you can autoload it, and then register it with
110+
the plugin
111+
manager <zend.view.helpers.register>.
112+
113+
Here is an example helper, which we're titling "SpecialPurpose"
114+
115+
```php
116+
// /module/src/MyModule/View/Helper/SpecialPurpose.php
117+
namespace MyModule\View\Helper;
118+
119+
use Zend\View\Helper\AbstractHelper;
120+
121+
class SpecialPurpose extends AbstractHelper
122+
{
123+
protected $count = 0;
124+
125+
public function __invoke()
126+
{
127+
$this->count++;
128+
$output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count);
129+
return htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
130+
}
131+
}
132+
```
133+
134+
Then assume that we \[register it with the plugin manager\](zend.view.helpers.register), by the name
135+
"specialpurpose".
136+
137+
Within a view script, you can call the `SpecialPurpose` helper as many times as you like; it will be
138+
instantiated once, and then it persists for the life of that `PhpRenderer` instance.
139+
140+
```php
141+
// remember, in a view script, $this refers to the Zend\View\Renderer\PhpRenderer instance.
142+
echo $this->specialPurpose();
143+
echo $this->specialPurpose();
144+
echo $this->specialPurpose();
145+
```
146+
147+
The output would look something like this:
148+
149+
```php
150+
I have seen 'The Jerk' 1 time(s).
151+
I have seen 'The Jerk' 2 time(s).
152+
I have seen 'The Jerk' 3 time(s).
153+
```
154+
155+
Sometimes you will need access to the calling `PhpRenderer` object -- for instance, if you need to
156+
use the registered encoding, or want to render another view script as part of your helper. This is
157+
why we define the `setView()` and `getView()` methods. As an example, we could rewrite the
158+
`SpecialPurpose` helper as follows to take advantage of the `EscapeHtml` helper:
159+
160+
```php
161+
namespace MyModule\View\Helper;
162+
163+
use Zend\View\Helper\AbstractHelper;
164+
165+
class SpecialPurpose extends AbstractHelper
166+
{
167+
protected $count = 0;
168+
169+
public function __invoke()
170+
{
171+
$this->count++;
172+
$output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count);
173+
$escaper = $this->getView()->plugin('escapehtml');
174+
return $escaper($output);
175+
}
176+
}
177+
```
178+
179+
## Registering Concrete Helpers
180+
181+
Sometimes it is convenient to instantiate a view helper, and then register it with the renderer.
182+
This can be done by injecting it directly into the plugin manager.
183+
184+
```php
185+
// $view is a PhpRenderer instance
186+
187+
$helper = new MyModule\View\Helper\LowerCase;
188+
// ...do some configuration or dependency injection...
189+
190+
$view->getHelperPluginManager()->setService('lowercase', $helper);
191+
```
192+
193+
The plugin manager will validate the helper/plugin, and if the validation passes, the helper/plugin
194+
will be registered.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# View Helper - BasePath
2+
3+
## Introduction
4+
5+
While most *URL*s generated by the framework have the base *URL* prepended automatically, developers
6+
will need to prepend the base *URL* to their own *URL*s (usually inside an href attribute) in order
7+
for paths to resources to be correct.
8+
9+
If you're running on ZF2's MVC base, `basePath()` will point to the `public` folder of the
10+
application's root.
11+
12+
## Basic Usage
13+
14+
Usage of the `basePath()` helper is straightforward:
15+
16+
```php
17+
/*
18+
* The following assume that the base URL of the page/application is "/mypage".
19+
*/
20+
21+
/*
22+
* Prints:
23+
* <base href="/mypage/" />
24+
*/
25+
<base href="<?php echo $this->basePath(); ?>" />
26+
27+
/*
28+
* Prints:
29+
* <link rel="stylesheet" type="text/css" href="/mypage/css/base.css" />
30+
*/
31+
<link rel="stylesheet" type="text/css"
32+
href="<?php echo $this->basePath('css/base.css'); ?>" />
33+
```
34+
35+
> ## Note
36+
For simplicity's sake, we strip out the entry *PHP* file (e.g., "`index.php`") from the base *URL* .
37+
However, in some situations this may cause a problem. If one occurs, use
38+
`$this-plugin('basePath')-setBasePath()` to manually set the base path.

doc/book/zend.view.helpers.cycle.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# View Helper - Cycle
2+
3+
## Introduction
4+
5+
The `Cycle` helper is used to alternate a set of values.
6+
7+
## Basic Usage
8+
9+
To add elements to cycle just specify them in constructor:
10+
11+
```php
12+
<table>
13+
<?php foreach ($this->books as $book): ?>
14+
<tr style="background-color: <?php echo $this->cycle(array('#F0F0F0', '#FFF'))
15+
->next() ?>">
16+
<td><?php echo $this->escapeHtml($book['author']) ?></td>
17+
</tr>
18+
<?php endforeach ?>
19+
</table>
20+
```
21+
22+
The output:
23+
24+
```php
25+
<table>
26+
<tr style="background-color: #F0F0F0">
27+
<td>First</td>
28+
</tr>
29+
<tr style="background-color: #FFF">
30+
<td>Second</td>
31+
</tr>
32+
</table>
33+
```
34+
35+
Or use `assign(array $data)` method and moving in backwards order:
36+
37+
```php
38+
<?php $this->cycle()->assign(array('#F0F0F0', '#FFF')) ?>
39+
40+
<table>
41+
<?php foreach ($this->books as $book): ?>
42+
<tr style="background-color: <?php echo $this->cycle()->prev() ?>">
43+
<td><?php echo $this->escapeHtml($book['author']) ?></td>
44+
</tr>
45+
<?php endforeach ?>
46+
</table>
47+
```
48+
49+
The output:
50+
51+
```php
52+
<table>
53+
<tr style="background-color: #FFF">
54+
<td>First</td>
55+
</tr>
56+
<tr style="background-color: #F0F0F0">
57+
<td>Second</td>
58+
</tr>
59+
</table>
60+
```
61+
62+
## Working with two or more cycles
63+
64+
To use two cycles you have to specify the names of cycles. Just set second parameter in cycle
65+
method: `$this->cycle(array('#F0F0F0', '#FFF'), 'cycle2')`
66+
67+
```php
68+
<table>
69+
<?php foreach ($this->books as $book): ?>
70+
<tr style="background-color: <?php echo $this->cycle(array('#F0F0F0', '#FFF'))
71+
->next() ?>">
72+
<td><?php echo $this->cycle(array(1, 2, 3), 'number')->next() ?></td>
73+
<td><?php echo $this->escapeHtml($book['author']) ?></td>
74+
</tr>
75+
<?php endforeach ?>
76+
</table>
77+
```
78+
79+
You can also use `assign($data, $name)` and `setName($name)` methods:
80+
81+
```php
82+
<?php
83+
$this->cycle()->assign(array('#F0F0F0', '#FFF'), 'colors');
84+
$this->cycle()->assign(array(1, 2, 3), 'numbers');
85+
?>
86+
<table>
87+
<?php foreach ($this->books as $book): ?>
88+
<tr style="background-color: <?php echo $this->cycle()->setName('colors')->next() ?>">
89+
<td><?php echo $this->cycle()->setName('numbers')->next() ?></td>
90+
<td><?php echo $this->escapeHtml($book['author']) ?></td>
91+
</tr>
92+
<?php endforeach ?>
93+
</table>
94+
```

0 commit comments

Comments
 (0)