我正在尝试根据页面内容将一些元素(以下格式)添加到我的页面的头部:
<meta property="og:title" content="some content" />
使用 headMeta() - &gt; appendName ,如下所示:
$this->view->headMeta()->appendName('og:title', 'some content');
在标题中生成以下内容:
<meta name="og:title" content="some content" />
有没有办法让Zend使用属性字段生成 meta ?
谢谢
答案 0 :(得分:8)
听起来你需要创建自己的视图助手,扩展标准的Zend Framework HeadMeta
视图助手,并实现一个名为appendProperty()
的方法,模仿appendName()
的行为。 / p>
由于似乎在appendName()
方法中处理了__call()
方法,看起来您的扩展类只能复制父类的__call()
形式,但更改了preg_match()
来自:
'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv)$/'
到
'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/'
[作为旁注,提交ZF跟踪器的问题可能是值得的,建议将此正则表达式模式从内联代码中拉出,然后将其放置为类的受保护成员。这样,一个子类 - 就像你的一样 - 可以简单地声明一个新的模式,而不是“复制”这么多的父代码。但在我向他们提出建议之前,我必须先看一下并测试一下。]
无论如何,只是在黑暗中刺伤......
更新:2010-12-17
我发现要使其工作需要更多。您需要覆盖受保护的成员$_typeKeys
和受保护的方法_normalizeType()
来处理您的新“属性”类型。
您的扩展课程可能如下所示:
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);
}
}
}
如前所述,如果在preg_match()
中检查的Zend_View_Helper_HeadMeta::__call()
模式被分解为名为$_callPattern
的受保护成员,则可能会短得多。然后扩展类不必复制__call()
方法的大部分内容。它只需覆盖受保护的成员$_typeKeys
和$_callPattern
并实施受保护的方法_normalizeType()
,如上所示。