15
15
use Zend \View \Exception ;
16
16
17
17
/**
18
- * Helper for printing breadcrumbs
18
+ * Helper for printing breadcrumbs.
19
19
*/
20
20
class Breadcrumbs extends AbstractHelper
21
21
{
22
22
/**
23
- * Whether last page in breadcrumb should be hyperlinked
23
+ * Whether last page in breadcrumb should be hyperlinked.
24
24
*
25
25
* @var bool
26
26
*/
27
27
protected $ linkLast = false ;
28
28
29
29
/**
30
- * The minimum depth a page must have to be included when rendering
30
+ * The minimum depth a page must have to be included when rendering.
31
31
*
32
32
* @var int
33
33
*/
34
34
protected $ minDepth = 1 ;
35
35
36
36
/**
37
- * Partial view script to use for rendering menu
37
+ * Partial view script to use for rendering menu.
38
38
*
39
39
* @var string|array
40
40
*/
41
41
protected $ partial ;
42
42
43
43
/**
44
- * Breadcrumbs separator string
44
+ * Breadcrumbs separator string.
45
45
*
46
46
* @var string
47
47
*/
48
48
protected $ separator = ' > ' ;
49
49
50
50
/**
51
- * Helper entry point
51
+ * Helper entry point.
52
52
*
53
53
* @param string|AbstractContainer $container container to operate on
54
54
* @return Breadcrumbs
@@ -63,7 +63,7 @@ public function __invoke($container = null)
63
63
}
64
64
65
65
/**
66
- * Renders helper
66
+ * Renders helper.
67
67
*
68
68
* Implements {@link HelperInterface::render()}.
69
69
*
@@ -83,7 +83,7 @@ public function render($container = null)
83
83
84
84
/**
85
85
* Renders breadcrumbs by chaining 'a' elements with the separator
86
- * registered in the helper
86
+ * registered in the helper.
87
87
*
88
88
* @param AbstractContainer $container [optional] container to render. Default is
89
89
* to render the container registered in the helper.
@@ -135,87 +135,53 @@ public function renderStraight($container = null)
135
135
}
136
136
137
137
/**
138
- * Renders the given $container by invoking the partial view helper
138
+ * Renders the given $container by invoking the partial view helper.
139
139
*
140
- * The container will simply be passed on as a model to the view script,
141
- * so in the script it will be available in <code>$this->container</code>.
140
+ * The container will simply be passed on as a model to the view script
141
+ * as-is, and will be available in the partial script as 'container', e.g.
142
+ * <code>echo 'Number of pages: ', count($this->container);</code>.
142
143
*
143
- * @param AbstractContainer $container [optional] container to pass to view script.
144
- * Default is to use the container registered
145
- * in the helper.
146
- * @param string|array $partial [optional] partial view script to use.
147
- * Default is to use the partial registered
148
- * in the helper. If an array is given, it
149
- * is expected to contain two values; the
150
- * partial view script to use, and the module
151
- * where the script can be found.
152
- * @throws Exception\RuntimeException if no partial provided
144
+ * @param null|AbstractContainer $container [optional] container to pass to view
145
+ * script. Default is to use the container registered in the helper.
146
+ * @param null|string|array $partial [optional] partial view script to use.
147
+ * Default is to use the partial registered in the helper. If an array
148
+ * is given, it is expected to contain two values; the partial view
149
+ * script to use, and the module where the script can be found.
150
+ * @return string
151
+ * @throws Exception\RuntimeException if no partial provided
153
152
* @throws Exception\InvalidArgumentException if partial is invalid array
154
- * @return string helper output
155
153
*/
156
154
public function renderPartial ($ container = null , $ partial = null )
157
155
{
158
- $ this ->parseContainer ($ container );
159
- if (null === $ container ) {
160
- $ container = $ this ->getContainer ();
161
- }
162
-
163
- if (null === $ partial ) {
164
- $ partial = $ this ->getPartial ();
165
- }
166
-
167
- if (empty ($ partial )) {
168
- throw new Exception \RuntimeException (
169
- 'Unable to render menu: No partial view script provided '
170
- );
171
- }
172
-
173
- // put breadcrumb pages in model
174
- $ model = [
175
- 'pages ' => [],
176
- 'separator ' => $ this ->getSeparator ()
177
- ];
178
- $ active = $ this ->findActive ($ container );
179
- if ($ active ) {
180
- $ active = $ active ['page ' ];
181
- $ model ['pages ' ][] = $ active ;
182
- while ($ parent = $ active ->getParent ()) {
183
- if ($ parent instanceof AbstractPage) {
184
- $ model ['pages ' ][] = $ parent ;
185
- } else {
186
- break ;
187
- }
188
-
189
- if ($ parent === $ container ) {
190
- // break if at the root of the given container
191
- break ;
192
- }
193
-
194
- $ active = $ parent ;
195
- }
196
- $ model ['pages ' ] = array_reverse ($ model ['pages ' ]);
197
- }
198
-
199
- /** @var \Zend\View\Helper\Partial $partialHelper */
200
- $ partialHelper = $ this ->view ->plugin ('partial ' );
201
-
202
- if (is_array ($ partial )) {
203
- if (count ($ partial ) != 2 ) {
204
- throw new Exception \InvalidArgumentException (
205
- 'Unable to render menu: A view partial supplied as '
206
- . 'an array must contain two values: partial view '
207
- . 'script and module where script can be found '
208
- );
209
- }
210
-
211
- return $ partialHelper ($ partial [0 ], $ model );
212
- }
156
+ return $ this ->renderPartialModel ([], $ container , $ partial );
157
+ }
213
158
214
- return $ partialHelper ($ partial , $ model );
159
+ /**
160
+ * Renders the given $container by invoking the partial view helper with the given parameters as the model.
161
+ *
162
+ * The container will simply be passed on as a model to the view script
163
+ * as-is, and will be available in the partial script as 'container', e.g.
164
+ * <code>echo 'Number of pages: ', count($this->container);</code>.
165
+ *
166
+ * Any parameters provided will be passed to the partial via the view model.
167
+ *
168
+ * @param null|AbstractContainer $container [optional] container to pass to view
169
+ * script. Default is to use the container registered in the helper.
170
+ * @param null|string|array $partial [optional] partial view script to use.
171
+ * Default is to use the partial registered in the helper. If an array
172
+ * is given, it is expected to contain two values; the partial view
173
+ * script to use, and the module where the script can be found.
174
+ * @return string
175
+ * @throws Exception\RuntimeException if no partial provided
176
+ * @throws Exception\InvalidArgumentException if partial is invalid array
177
+ */
178
+ public function renderPartialWithParams (array $ params = [], $ container = null , $ partial = null )
179
+ {
180
+ return $ this ->renderPartialModel ($ params , $ container , $ partial );
215
181
}
216
182
217
183
/**
218
- * Sets whether last page in breadcrumbs should be hyperlinked
184
+ * Sets whether last page in breadcrumbs should be hyperlinked.
219
185
*
220
186
* @param bool $linkLast whether last page should be hyperlinked
221
187
* @return Breadcrumbs
@@ -227,7 +193,7 @@ public function setLinkLast($linkLast)
227
193
}
228
194
229
195
/**
230
- * Returns whether last page in breadcrumbs should be hyperlinked
196
+ * Returns whether last page in breadcrumbs should be hyperlinked.
231
197
*
232
198
* @return bool
233
199
*/
@@ -237,26 +203,23 @@ public function getLinkLast()
237
203
}
238
204
239
205
/**
240
- * Sets which partial view script to use for rendering menu
206
+ * Sets which partial view script to use for rendering menu.
241
207
*
242
208
* @param string|array $partial partial view script or null. If an array is
243
- * given, it is expected to contain two
244
- * values; the partial view script to use,
245
- * and the module where the script can be
246
- * found.
209
+ * given, it is expected to contain two values; the partial view script
210
+ * to use, and the module where the script can be found.
247
211
* @return Breadcrumbs
248
212
*/
249
213
public function setPartial ($ partial )
250
214
{
251
215
if (null === $ partial || is_string ($ partial ) || is_array ($ partial )) {
252
216
$ this ->partial = $ partial ;
253
217
}
254
-
255
218
return $ this ;
256
219
}
257
220
258
221
/**
259
- * Returns partial view script to use for rendering menu
222
+ * Returns partial view script to use for rendering menu.
260
223
*
261
224
* @return string|array|null
262
225
*/
@@ -266,7 +229,7 @@ public function getPartial()
266
229
}
267
230
268
231
/**
269
- * Sets breadcrumb separator
232
+ * Sets breadcrumb separator.
270
233
*
271
234
* @param string $separator separator string
272
235
* @return Breadcrumbs
@@ -281,12 +244,73 @@ public function setSeparator($separator)
281
244
}
282
245
283
246
/**
284
- * Returns breadcrumb separator
247
+ * Returns breadcrumb separator.
285
248
*
286
- * @return string breadcrumb separator
249
+ * @return string breadcrumb separator
287
250
*/
288
251
public function getSeparator ()
289
252
{
290
253
return $ this ->separator ;
291
254
}
255
+
256
+ /**
257
+ * Render a partial with the given "model".
258
+ *
259
+ * @param array $params
260
+ * @param null|AbstractContainer $container
261
+ * @param null|string|array $partial
262
+ * @return string
263
+ * @throws Exception\RuntimeException if no partial provided
264
+ * @throws Exception\InvalidArgumentException if partial is invalid array
265
+ */
266
+ protected function renderPartialModel (array $ params , $ container , $ partial )
267
+ {
268
+ $ this ->parseContainer ($ container );
269
+ if (null === $ container ) {
270
+ $ container = $ this ->getContainer ();
271
+ }
272
+ if (null === $ partial ) {
273
+ $ partial = $ this ->getPartial ();
274
+ }
275
+ if (empty ($ partial )) {
276
+ throw new Exception \RuntimeException (
277
+ 'Unable to render breadcrumbs: No partial view script provided '
278
+ );
279
+ }
280
+ $ model = array_merge ($ params , ['pages ' => []], ['separator ' => $ this ->getSeparator ()]);
281
+ $ active = $ this ->findActive ($ container );
282
+ if ($ active ) {
283
+ $ active = $ active ['page ' ];
284
+ $ model ['pages ' ][] = $ active ;
285
+ while ($ parent = $ active ->getParent ()) {
286
+ if (! $ parent instanceof AbstractPage) {
287
+ break ;
288
+ }
289
+
290
+ $ model ['pages ' ][] = $ parent ;
291
+ if ($ parent === $ container ) {
292
+ // break if at the root of the given container
293
+ break ;
294
+ }
295
+ $ active = $ parent ;
296
+ }
297
+ $ model ['pages ' ] = array_reverse ($ model ['pages ' ]);
298
+ }
299
+
300
+ /** @var \Zend\View\Helper\Partial $partialHelper */
301
+ $ partialHelper = $ this ->view ->plugin ('partial ' );
302
+ if (is_array ($ partial )) {
303
+ if (count ($ partial ) != 2 ) {
304
+ throw new Exception \InvalidArgumentException (
305
+ 'Unable to render breadcrumbs: A view partial supplied as '
306
+ . 'an array must contain two values: partial view '
307
+ . 'script and module where script can be found '
308
+ );
309
+ }
310
+
311
+ return $ partialHelper ($ partial [0 ], $ model );
312
+ }
313
+
314
+ return $ partialHelper ($ partial , $ model );
315
+ }
292
316
}
0 commit comments