I'm trying to add some meta (in the following format) to the head of my pages according to the page content:
<meta property="og:title" content="some content" />
Using the headMeta()->appendName like this:
$this->view->headMeta()->appendName('og:title', 'some content');
generates the following in the header:
<meta name="og:title" content="some content" />
Is there a way to make Zend generates meta with the property field?
Thank you
Sounds like you need to create your own view-helper, extend the standard Zend Framework HeadMeta
view helper, and implementing a method called appendProperty()
, mimicking the behavior of appendName()
.
Since the appendName()
method seems to be handled in the __call()
method, it looks like your extended class could simply copy the same __call()
form the parent, but change the pattern used in the preg_match()
from:
'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/'
to
'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/'
[As a side note, it might be worthwhile to file an issue with the ZF tracker, recommending that this regex pattern is pulled out of the inline code and placed instead it as a protected member of the class. This way, a subclass - like yours - could simply declare a new pattern, rather than "duplicating" so much of the parent code. But I'd have to look and test a bit more before I suggest that to them.]
Anyway, just a stab in the dark...
Update: 2010-12-17
I discovered that a bit more is required to make it work. You need to override the protected member $_typeKeys
and the protected method _normalizeType()
to deal with your new "Property" type.
Your extended class could look something like this:
class Kwis_View_Helper_HeadMeta extends Zend_View_Helper_HeadMeta
{
protected $_typeKeys = array('name', 'http-equiv', 'charset', 'property');
public function __call($method, $args)
{
if (preg_match('/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/', $method, $matches)) {
$action = $matches['action'];
$type = $this->_normalizeType($matches['type']);
$argc = count($args);
$index = null;
if ('offsetSet' == $action) {
if (0 < $argc) {
$index = array_shift($args);
--$argc;
}
}
if (2 > $argc) {
require_once 'Zend/View/Exception.php';
$e = new Zend_View_Exception('Too few arguments provided; requires key value, and content');
$e->setView($this->view);
throw $e;
}
if (3 > $argc) {
$args[] = array();
}
$item = $this->createData($type, $args[0], $args[1], $args[2]);
if ('offsetSet' == $action) {
return $this->offsetSet($index, $item);
}
$this->$action($item);
return $this;
}
return parent::__call($method, $args);
}
protected function _normalizeType($type)
{
switch ($type) {
case 'Property':
return 'property';
default:
return parent::_normalizeType($type);
}
}
}
As observed previously, this could be so much shorter if the preg_match()
pattern checked in Zend_View_Helper_HeadMeta::__call()
was factored out into a protected member called something like $_callPattern
. Then the extended class would not have to duplicate the bulk of the __call()
method. It would only have to override the protected members $_typeKeys
and $_callPattern
and implement the protected method _normalizeType()
, as shown above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With