diff --git a/src/Helper/Navigation/Breadcrumbs.php b/src/Helper/Navigation/Breadcrumbs.php
index d72c9ecc..3b7d2fe7 100644
--- a/src/Helper/Navigation/Breadcrumbs.php
+++ b/src/Helper/Navigation/Breadcrumbs.php
@@ -1,12 +1,13 @@
htmlify($parent)
- . $this->getSeparator()
- . $html;
+ .$this->getSeparator()
+ .$html;
}
if ($parent === $container) {
@@ -131,103 +135,82 @@ public function renderStraight($container = null)
$active = $parent;
}
- return strlen($html) ? $this->getIndent() . $html : '';
+ return strlen($html) ? $this->getIndent().$html : '';
}
/**
- * Renders the given $container by invoking the partial view helper
- *
- * The container will simply be passed on as a model to the view script,
- * so in the script it will be available in $this->container
.
- *
- * @param AbstractContainer $container [optional] container to pass to view script.
- * Default is to use the container registered
- * in the helper.
- * @param string|array $partial [optional] partial view script to use.
- * Default is to use the partial registered
- * in the helper. If an array is given, it
- * is expected to contain two values; the
- * partial view script to use, and the module
- * where the script can be found.
- * @throws Exception\RuntimeException if no partial provided
+ * Renders the given $container by invoking the partial view helper.
+ *
+ * The container will simply be passed on as a model to the view script
+ * as-is, and will be available in the partial script as 'container', e.g.
+ * echo 'Number of pages: ', count($this->container);
.
+ *
+ * @param null|AbstractContainer $container [optional] container to pass to view
+ * script. Default is to use the container
+ * registered in the helper.
+ * @param null|string|array $partial [optional] partial view script to use.
+ * Default is to use the partial
+ * registered in the helper. If an array
+ * is given, it is expected to contain two
+ * values; the partial view script to use,
+ * and the module where the script can be
+ * found.
+ *
+ * @return string
+ *
+ * @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
- * @return string helper output
*/
public function renderPartial($container = null, $partial = null)
{
- $this->parseContainer($container);
- if (null === $container) {
- $container = $this->getContainer();
- }
-
- if (null === $partial) {
- $partial = $this->getPartial();
- }
-
- if (empty($partial)) {
- throw new Exception\RuntimeException(
- 'Unable to render menu: No partial view script provided'
- );
- }
-
- // put breadcrumb pages in model
- $model = [
- 'pages' => [],
- 'separator' => $this->getSeparator()
- ];
- $active = $this->findActive($container);
- if ($active) {
- $active = $active['page'];
- $model['pages'][] = $active;
- while ($parent = $active->getParent()) {
- if ($parent instanceof AbstractPage) {
- $model['pages'][] = $parent;
- } else {
- break;
- }
-
- if ($parent === $container) {
- // break if at the root of the given container
- break;
- }
-
- $active = $parent;
- }
- $model['pages'] = array_reverse($model['pages']);
- }
-
- /** @var \Zend\View\Helper\Partial $partialHelper */
- $partialHelper = $this->view->plugin('partial');
-
- if (is_array($partial)) {
- if (count($partial) != 2) {
- throw new Exception\InvalidArgumentException(
- 'Unable to render menu: A view partial supplied as '
- . 'an array must contain two values: partial view '
- . 'script and module where script can be found'
- );
- }
-
- return $partialHelper($partial[0], $model);
- }
-
- return $partialHelper($partial, $model);
+ return $this->renderPartialModel([], $container, $partial);
+ }
+ /**
+ * Renders the given $container by invoking the partial view helper with the given parameters as the model.
+ *
+ * The container will simply be passed on as a model to the view script
+ * as-is, and will be available in the partial script as 'container', e.g.
+ * echo 'Number of pages: ', count($this->container);
.
+ *
+ * Any parameters provided will be passed to the partial via the view model.
+ *
+ * @param null|AbstractContainer $container [optional] container to pass to view
+ * script. Default is to use the container
+ * registered in the helper.
+ * @param null|string|array $partial [optional] partial view script to use.
+ * Default is to use the partial
+ * registered in the helper. If an array
+ * is given, it is expected to contain two
+ * values; the partial view script to use,
+ * and the module where the script can be
+ * found.
+ *
+ * @return string
+ *
+ * @throws Exception\RuntimeException if no partial provided
+ * @throws Exception\InvalidArgumentException if partial is invalid array
+ */
+ public function renderPartialWithParams(array $params = [], $container = null, $partial = null)
+ {
+ return $this->renderPartialModel($params, $container, $partial);
}
/**
- * Sets whether last page in breadcrumbs should be hyperlinked
+ * Sets whether last page in breadcrumbs should be hyperlinked.
+ *
+ * @param bool $linkLast whether last page should be hyperlinked
*
- * @param bool $linkLast whether last page should be hyperlinked
* @return Breadcrumbs
*/
public function setLinkLast($linkLast)
{
$this->linkLast = (bool) $linkLast;
+
return $this;
}
/**
- * Returns whether last page in breadcrumbs should be hyperlinked
+ * Returns whether last page in breadcrumbs should be hyperlinked.
*
* @return bool
*/
@@ -237,13 +220,14 @@ public function getLinkLast()
}
/**
- * Sets which partial view script to use for rendering menu
+ * Sets which partial view script to use for rendering menu.
+ *
+ * @param string|array $partial partial view script or null. If an array is
+ * given, it is expected to contain two
+ * values; the partial view script to use,
+ * and the module where the script can be
+ * found.
*
- * @param string|array $partial partial view script or null. If an array is
- * given, it is expected to contain two
- * values; the partial view script to use,
- * and the module where the script can be
- * found.
* @return Breadcrumbs
*/
public function setPartial($partial)
@@ -256,7 +240,7 @@ public function setPartial($partial)
}
/**
- * Returns partial view script to use for rendering menu
+ * Returns partial view script to use for rendering menu.
*
* @return string|array|null
*/
@@ -266,9 +250,10 @@ public function getPartial()
}
/**
- * Sets breadcrumb separator
+ * Sets breadcrumb separator.
+ *
+ * @param string $separator separator string
*
- * @param string $separator separator string
* @return Breadcrumbs
*/
public function setSeparator($separator)
@@ -281,12 +266,74 @@ public function setSeparator($separator)
}
/**
- * Returns breadcrumb separator
+ * Returns breadcrumb separator.
*
- * @return string breadcrumb separator
+ * @return string breadcrumb separator
*/
public function getSeparator()
{
return $this->separator;
}
+
+ /**
+ * Render a partial with the given "model".
+ *
+ * @param array $params
+ * @param null|AbstractContainer $container
+ * @param null|string|array $partial
+ *
+ * @return string
+ *
+ * @throws Exception\RuntimeException if no partial provided
+ * @throws Exception\InvalidArgumentException if partial is invalid array
+ */
+ protected function renderPartialModel(array $params, $container, $partial)
+ {
+ $this->parseContainer($container);
+ if (null === $container) {
+ $container = $this->getContainer();
+ }
+ if (null === $partial) {
+ $partial = $this->getPartial();
+ }
+ if (empty($partial)) {
+ throw new Exception\RuntimeException(
+ 'Unable to render breadcrumbs: No partial view script provided'
+ );
+ }
+ $model = array_merge($params, ['pages' => []], ['separator' => $this->getSeparator()]);
+ $active = $this->findActive($container);
+ if ($active) {
+ $active = $active['page'];
+ $model['pages'][] = $active;
+ while ($parent = $active->getParent()) {
+ if ($parent instanceof AbstractPage) {
+ $model['pages'][] = $parent;
+ } else {
+ break;
+ }
+ if ($parent === $container) {
+ // break if at the root of the given container
+ break;
+ }
+ $active = $parent;
+ }
+ $model['pages'] = array_reverse($model['pages']);
+ }
+ /** @var \Zend\View\Helper\Partial $partialHelper */
+ $partialHelper = $this->view->plugin('partial');
+ if (is_array($partial)) {
+ if (count($partial) != 2) {
+ throw new Exception\InvalidArgumentException(
+ 'Unable to render breadcrumbs: A view partial supplied as '
+ .'an array must contain two values: partial view '
+ .'script and module where script can be found'
+ );
+ }
+
+ return $partialHelper($partial[0], $model);
+ }
+
+ return $partialHelper($partial, $model);
+ }
}
diff --git a/src/Helper/Navigation/Menu.php b/src/Helper/Navigation/Menu.php
index 90b053ea..88e3113d 100644
--- a/src/Helper/Navigation/Menu.php
+++ b/src/Helper/Navigation/Menu.php
@@ -1,12 +1,13 @@
element
+ * Whether page class should be applied to
echo 'Number of pages: ', count($this->container);
.
*
- * @param AbstractContainer $container [optional] container to pass to view
- * script. Default is to use the container
- * registered in the helper.
- * @param string|array $partial [optional] partial view script to use.
- * Default is to use the partial
- * registered in the helper. If an array
- * is given, it is expected to contain two
- * values; the partial view script to use,
- * and the module where the script can be
- * found.
+ * @param null|AbstractContainer $container [optional] container to pass to view
+ * script. Default is to use the container
+ * registered in the helper.
+ * @param null|string|array $partial [optional] partial view script to use.
+ * Default is to use the partial
+ * registered in the helper. If an array
+ * is given, it is expected to contain two
+ * values; the partial view script to use,
+ * and the module where the script can be
+ * found.
+ *
* @return string
- * @throws Exception\RuntimeException if no partial provided
+ *
+ * @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
*/
public function renderPartial($container = null, $partial = null)
{
- $this->parseContainer($container);
- if (null === $container) {
- $container = $this->getContainer();
- }
-
- if (null === $partial) {
- $partial = $this->getPartial();
- }
-
- if (empty($partial)) {
- throw new Exception\RuntimeException(
- 'Unable to render menu: No partial view script provided'
- );
- }
-
- $model = [
- 'container' => $container
- ];
-
- /** @var \Zend\View\Helper\Partial $partialHelper */
- $partialHelper = $this->view->plugin('partial');
-
- if (is_array($partial)) {
- if (count($partial) != 2) {
- throw new Exception\InvalidArgumentException(
- 'Unable to render menu: A view partial supplied as '
- . 'an array must contain two values: partial view '
- . 'script and module where script can be found'
- );
- }
-
- return $partialHelper($partial[0], $model);
- }
-
- return $partialHelper($partial, $model);
+ return $this->renderPartialModel([], $container, $partial);
+ }
+ /**
+ * Renders the given $container by invoking the partial view helper with the given parameters as the model.
+ *
+ * The container will simply be passed on as a model to the view script
+ * as-is, and will be available in the partial script as 'container', e.g.
+ * echo 'Number of pages: ', count($this->container);
.
+ *
+ * Any parameters provided will be passed to the partial via the view model.
+ *
+ * @param null|AbstractContainer $container [optional] container to pass to view
+ * script. Default is to use the container
+ * registered in the helper.
+ * @param null|string|array $partial [optional] partial view script to use.
+ * Default is to use the partial
+ * registered in the helper. If an array
+ * is given, it is expected to contain two
+ * values; the partial view script to use,
+ * and the module where the script can be
+ * found.
+ *
+ * @return string
+ *
+ * @throws Exception\RuntimeException if no partial provided
+ * @throws Exception\InvalidArgumentException if partial is invalid array
+ */
+ public function renderPartialWithParams(array $params = [], $container = null, $partial = null)
+ {
+ return $this->renderPartialModel($params, $container, $partial);
}
-
/**
- * Renders the inner-most sub menu for the active page in the $container
+ * Renders the inner-most sub menu for the active page in the $container.
*
* This is a convenience method which is equivalent to the following call:
*
@@ -449,23 +419,24 @@ public function renderPartial($container = null, $partial = null)
* ));
*
*
- * @param AbstractContainer $container [optional] container to
- * render. Default is to render
- * the container registered in
- * the helper.
- * @param string $ulClass [optional] CSS class to
- * use for UL element. Default
- * is to use the value from
- * {@link getUlClass()}.
- * @param string|int $indent [optional] indentation as
- * a string or number of
- * spaces. Default is to use
- * the value retrieved from
- * {@link getIndent()}.
- * @param string $liActiveClass [optional] CSS class to
- * use for UL element. Default
- * is to use the value from
- * {@link getUlClass()}.
+ * @param AbstractContainer $container [optional] container to
+ * render. Default is to render
+ * the container registered in
+ * the helper.
+ * @param string $ulClass [optional] CSS class to
+ * use for UL element. Default
+ * is to use the value from
+ * {@link getUlClass()}.
+ * @param string|int $indent [optional] indentation as
+ * a string or number of
+ * spaces. Default is to use
+ * the value retrieved from
+ * {@link getIndent()}.
+ * @param string $liActiveClass [optional] CSS class to
+ * use for UL element. Default
+ * is to use the value from
+ * {@link getUlClass()}.
+ *
* @return string
*/
public function renderSubMenu(
@@ -483,19 +454,19 @@ public function renderSubMenu(
'renderParents' => false,
'escapeLabels' => true,
'addClassToListItem' => false,
- 'liActiveClass' => $liActiveClass
+ 'liActiveClass' => $liActiveClass,
]);
}
-
/**
* Returns an HTML string containing an 'a' element for the given page if
- * the page's href is not empty, and a 'span' element if it is empty
+ * the page's href is not empty, and a 'span' element if it is empty.
*
* Overrides {@link AbstractHelper::htmlify()}.
*
- * @param AbstractPage $page page to generate HTML for
- * @param bool $escapeLabel Whether or not to escape the label
- * @param bool $addClassToListItem Whether or not to add the page class to the list item
+ * @param AbstractPage $page page to generate HTML for
+ * @param bool $escapeLabel Whether or not to escape the label
+ * @param bool $addClassToListItem Whether or not to add the page class to the list item
+ *
* @return string
*/
public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToListItem = false)
@@ -505,11 +476,9 @@ public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToList
'id' => $page->getId(),
'title' => $this->translate($page->getTitle(), $page->getTextDomain()),
];
-
if ($addClassToListItem === false) {
$attribs['class'] = $page->getClass();
}
-
// does page have a href?
$href = $page->getHref();
if ($href) {
@@ -519,8 +488,7 @@ public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToList
} else {
$element = 'span';
}
-
- $html = '<' . $element . $this->htmlAttribs($attribs) . '>';
+ $html = '<'.$element.$this->htmlAttribs($attribs).'>';
$label = $this->translate($page->getLabel(), $page->getTextDomain());
if ($escapeLabel === true) {
/** @var \Zend\View\Helper\EscapeHtml $escaper */
@@ -529,15 +497,15 @@ public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToList
} else {
$html .= $label;
}
- $html .= '' . $element . '>';
+ $html .= ''.$element.'>';
return $html;
}
-
/**
- * Normalizes given render options
+ * Normalizes given render options.
+ *
+ * @param array $options [optional] options to normalize
*
- * @param array $options [optional] options to normalize
* @return array
*/
protected function normalizeOptions(array $options = [])
@@ -547,13 +515,11 @@ protected function normalizeOptions(array $options = [])
} else {
$options['indent'] = $this->getIndent();
}
-
if (isset($options['ulClass']) && $options['ulClass'] !== null) {
$options['ulClass'] = (string) $options['ulClass'];
} else {
$options['ulClass'] = $this->getUlClass();
}
-
if (array_key_exists('minDepth', $options)) {
if (null !== $options['minDepth']) {
$options['minDepth'] = (int) $options['minDepth'];
@@ -561,11 +527,9 @@ protected function normalizeOptions(array $options = [])
} else {
$options['minDepth'] = $this->getMinDepth();
}
-
if ($options['minDepth'] < 0 || $options['minDepth'] === null) {
$options['minDepth'] = 0;
}
-
if (array_key_exists('maxDepth', $options)) {
if (null !== $options['maxDepth']) {
$options['maxDepth'] = (int) $options['maxDepth'];
@@ -573,23 +537,18 @@ protected function normalizeOptions(array $options = [])
} else {
$options['maxDepth'] = $this->getMaxDepth();
}
-
if (!isset($options['onlyActiveBranch'])) {
$options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
}
-
if (!isset($options['escapeLabels'])) {
$options['escapeLabels'] = $this->escapeLabels;
}
-
if (!isset($options['renderParents'])) {
$options['renderParents'] = $this->getRenderParents();
}
-
if (!isset($options['addClassToListItem'])) {
$options['addClassToListItem'] = $this->getAddClassToListItem();
}
-
if (isset($options['liActiveClass']) && $options['liActiveClass'] !== null) {
$options['liActiveClass'] = (string) $options['liActiveClass'];
} else {
@@ -598,58 +557,59 @@ protected function normalizeOptions(array $options = [])
return $options;
}
-
/**
- * Sets a flag indicating whether labels should be escaped
+ * Sets a flag indicating whether labels should be escaped.
*
* @param bool $flag [optional] escape labels
+ *
* @return self
*/
public function escapeLabels($flag = true)
{
$this->escapeLabels = (bool) $flag;
+
return $this;
}
-
/**
- * Enables/disables page class applied to