-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathpresenter.html
262 lines (242 loc) · 8.65 KB
/
presenter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./../assets/css/combined.css?20170912">
<link rel="shortcut icon" href="./../favicon.ico" />
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
var path = './../';
var class_prefix = "Presenter::";
</script>
<script src="./../assets/js/combined.js?20170912"></script>
<title>Presenter - Classes - FuelPHP Documentation</title>
</head>
<body>
<div id="container">
<header id="header">
<div class="table">
<div id="cse">
<gcse:searchbox-only newWindow="true"></gcse:searchbox-only>
</div>
<h1>
<a href="https://fuelphp.com"><img height="37px" width="147px" src="./../assets/img/fuel.png" /></a>
<strong>Documentation</strong>
</h1>
</div>
<nav>
<div class="clear"></div>
</nav>
<a href="#" id="toc_handle">table of contents</a>
<div class="clear"></div>
</header>
<div id="main">
<h2>Presenter Class</h2>
<p>
The Presenter class acts as an object wrapper for "views", and is used to abstract all logic
related to the view away from the controller. <a href="../general/presenters.html">Read more about using presenters.</a>
Like a Controller, a Presenter supports <kbd>before()</kbd> and <kbd>after()</kbd> methods, which you can use for code generic to all methods for view prepping.
</p>
<article>
<h4 class="method" id="method_forge">forge($presenter, $method = 'view', $auto_filter = null, $view = null)</h4>
<p>The <strong>forge</strong> method returns a new Presenter object.</p>
<table class="method">
<tbody>
<tr>
<th>Static</th>
<td>Yes</td>
</tr>
<tr>
<th>Parameters</th>
<td>
<table class="parameters">
<tr>
<th>Param</th>
<th>Default</th>
<th class="description">Description</th>
</tr>
<tr>
<th><kbd>$presenter</kbd></th>
<td><em>required</em></td>
<td class="description">Name of the presenter, and by default of its associated view, using <a href="./view.html#method_forge">View</a> notation.</td>
</tr>
<tr>
<th><kbd>$method</kbd></th>
<td><pre class="php"><code>'view'</code></pre></td>
<td class="description">Name of the presenter method that will prep the View for rendering. You can have multiple prep methods defined in the Presenter, for example to generate different layouts of the same view.</td>
</tr>
<tr>
<th><kbd>$auto_filter</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td class="description">set to <em>true</em> or <em>false</em> to set auto encoding, defaults to main config setting (app/config/config.php)</td>
</tr>
<tr>
<th><kbd>$view</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td class="description">Custom view name, used if the view to be loaded can not be determined from the presenter name</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>Returns</th>
<td>a new <em>Presenter</em> object</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>// Will create a Presenter object
// for the APPPATH/views/admin/index.php view file
// using the Presenter_Admin_Index class in APPPATH/classes/presenter/admin/index.php
$presenter = Presenter::Forge('admin/index');
// use the custom() method in the presenter to render the view differently
$presenter = Presenter::Forge('admin/index', 'custom');
// use a custom view
$presenter = Presenter::Forge('admin/index', 'custom', null, 'admin/different/view');
// or even a custom view object
$view = View::forge('admin/different/view', array(
'menu' => $menu,
'articles' => $articles,
'footer_links' => $footer_links,
));
$presenter = Presenter::Forge('admin/index', 'custom', null, $view);</code></pre>
</td>
</tr>
</tbody>
</table>
</article>
<article>
<h4 class="method" id="method_get_view">get_view()</h4>
<p>The <strong>get_view</strong> method returns the View instance associated with the Presenter object.</p>
<table class="method">
<tbody>
<tr>
<th>Static</th>
<td>No</td>
</tr>
<tr>
<th>Parameters</th>
<td>
None
</td>
</tr>
<tr>
<th>Returns</th>
<td>The associated View object</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>// create a presenter instance
$presenter = Presenter::Forge('admin/index');
// and the view associated with it
$view = $presenter->get_view();</code></pre>
</td>
</tr>
</tbody>
</table>
</article>
<article>
<h4 class="method" id="method_view">view()</h4>
<p>The <strong>view</strong> method is the default method which is called when the Presenter is rendered. It contains the logic to prep the view for rendering.</p>
<table class="method">
<tbody>
<tr>
<th>Static</th>
<td>No</td>
</tr>
<tr>
<th>Parameters</th>
<td>
None
</td>
<tr>
<th>Example</th>
<td>
See the <a href="../general/presenter.html">Presenter</a> overview page.
</td>
</tr>
</tr>
</tbody>
</table>
<p class="note">
A presenter can contain multiple prepping methods, which are used when you need multiple sets of logic for generating the view.
You could for example have a custom method that generates the view without headers and footers, or one that creates a custom
view optimized for mobile devices. It allows you to keep the controller generic, it doesn't need to know what output has to be
generated by the presenter.
</p>
</article>
<article>
<h4 class="method" id="method_set_view">set_view($view = null)</h4>
<p>The <strong>set_view</strong> method can be used to change the View associated with the presenter.</p>
<table class="method">
<tbody>
<tr>
<th>Static</th>
<td>No</td>
</tr>
<tr>
<th>Parameters</th>
<td>
<table class="parameters">
<tr>
<th>Param</th>
<th>Default</th>
<th class="description">Description</th>
</tr>
<tr>
<th><kbd>$view</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td class="description">Name of the view file, or a valid View object.</td>
</tr>
</table>
</td>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>// create a presenter instance
$presenter = Presenter::Forge('admin/public');
// some code here that changes the associated view
if ($something)
{
$presenter->set_view('admin/member');
}</code></pre>
</td>
</tr>
</tr>
</tbody>
</table>
<p class="note">
When you use this method, any variables set on or bound to the original View object will be transferred to the new View.
</p>
</article>
<h3 id="view">View object compatibility</h3>
<p>
The Presenter class is interchangeable with the View class in your code. This means that if you start with Views, and later
realize you need to have additional view prepping logic and you want to use a Presenter instead, you don't have to change
your controller code, other than forging a Presenter instead of a View.
</p>
<p>
To support this, the Presenter exposes the <kbd>set()</kbd>, <kbd>set_safe()</kbd>, <kbd>bind()</kbd>, <kbd>auto_filter()</kbd> and
<kbd>render()</kbd> methods of the associated View object. Is also has magic getter and setters to access and set properties on the
associated View object.
</p>
<p>
The Presenter doesn't support the static methods <kbd>set_global()</kbd> and <kbd>bind_global()</kbd>, if you need global variables
for your views, you still have to set them on the View class. For the Presenter, this is transparent.
</p>
<p class="note">
If you want to extend the Presenter to be able to swap View instances after the Presenter object has been created, know that the
presenter doesn't have its own data container. Instead, it uses the associated View object to store all data, which means that
if you swap that View object by a new one, you lose all variables set on it!
</p>
</div>
<footer>
<p>
© FuelPHP Development Team 2010-2025 - <a href="https://fuelphp.com">FuelPHP</a> is released under the MIT license.
</p>
</footer>
</div>
</body>
</html>