Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save balupton/539149 to your computer and use it in GitHub Desktop.
Save balupton/539149 to your computer and use it in GitHub Desktop.
Navigation Menu View Helper does not support templates.
--- zend-1.10.3-lib/Zend/View/Helper/Navigation/Menu.php 2010-04-02 17:08:46.000000000 +0800
+++ balphp-lib/Zend/View/Helper/Navigation/Menu.php 2010-04-29 23:32:42.000000000 +0800
@@ -198,6 +198,31 @@ class Zend_View_Helper_Navigation_Menu
// Public methods:
/**
+ * Simple template to use in the htmlify process if specified
+ *
+ * @var string
+ */
+ protected $_template = null;
+
+ /**
+ * Set a simple template to use in the htmlify process
+ * @param string $template
+ * @return $this
+ */
+ public function setTemplate ( $template ) {
+ $this->_template = $template;
+ return $this;
+ }
+
+ /**
+ * Get the simple template to use in the htmlify process if specified
+ * @return string
+ */
+ public function getTemplate ( ) {
+ return $this->_template;
+ }
+
+ /**
* 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
*
@@ -238,8 +263,23 @@ class Zend_View_Helper_Navigation_Menu
$element = 'span';
}
+ // put together
+ $template = $this->getTemplate();
+ if ( !$template ) {
+ $renderedTemplate = $this->view->escape($label);
+ } else {
+ $renderedTemplate = populate($template,
+ array(
+ 'label' => $this->view->escape($label),
+ 'title' => $this->view->escape($title),
+ 'Page' => $page
+ )
+ );
+ }
+
+ // return
return '<' . $element . $this->_htmlAttribs($attribs) . '>'
- . $this->view->escape($label)
+ . $renderedTemplate
. '</' . $element . '>';
}
@@ -645,3 +685,4 @@ class Zend_View_Helper_Navigation_Menu
}
}
}
+
<?php
// This is the populate function mentioned in the patch.
// Included here as was unsure where the best place to put it in Zend Framework would be.
/**
* Populates some text with a set of params
* @version 1, January 30, 2010
* @param array $array
* @return mixed
* @author Benjamin "balupton" Lupton <[email protected]> - {@link http://www.balupton.com/}
*/
function populate ( $text, $params = null, $sprintf = null ) {
# Prepare
$result = '';
# Ensure params is an array
if ( !$params ) $params = array(); elseif ( !is_array($params) ) $params = array($params);
# Use sprintf?
if ( $sprintf === null ) {
# Detect
$sprintf = is_simple_array($params);
}
# Apply sprintf?
if ( $sprintf ) {
# Populate simple
$result = vsprintf($text, $params);
}
else {
# Populate advanced
$result = ' '.$text;
$result = preg_replace('/([^\\\\])?\\$([a-zA-Z0-9_\\.]+)/ie', 'preg_unescape("${1}") . delve(\\$params, preg_unescape("${2}"))', $result);
$result = substr($result,1);
$result = str_replace(array('\\$','\\.'), array('$','.'), $result);
}
# Return result
return $result;
}
/**
* Unescape a string a pregex replace function
* @version 1, February 01, 2010
* @param string $value
* @return string
* @author Benjamin "balupton" Lupton <[email protected]> - {@link http://www.balupton.com/}
*/
function preg_unescape ( $value ) {
/*
* When using the e modifier, this function escapes some characters (namely ', ", \ and null) in the strings that replace the backreferences.
* This is done to ensure that no syntax errors arise from backreference usage with either single or double quotes (e.g. 'strlen(\'$1\')+strlen("$2")').
* Make sure you are aware of PHP's string syntax to know exactly how the interpreted string will look like.
*/
$result = str_replace(array("\\'", '\\"', '\\\\', '\\0'), array("'", '"', '\\', '\0'), $value);
return $result;
}
/**
* Checks if the array is a simple array
* @version 1, January 30, 2010
* @param array $array
* @return mixed
* @author Benjamin "balupton" Lupton <[email protected]> - {@link http://www.balupton.com/}
*/
function is_simple_array ( array $array ) {
return is_numeric(implode('',array_keys($array)));
}
<?=$this->menu('front.actions')
->setTemplate('<span class="title">$label</span>')
->render()?>
@balupton
Copy link
Author

Related Zend Framework Issue:
http://framework.zend.com/issues/browse/ZF-10346

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment