Zend框架:元属性集成 [英] Zend framework: Meta property integration

查看:54
本文介绍了Zend框架:元属性集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据页面内容在页面顶部添加一些元信息(以下格式):

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" />

像这样使用 headMeta()-> appendName :

$this->view->headMeta()->appendName('og:title', 'some content');

在标题中生成以下内容:

generates the following in the header:

<meta name="og:title" content="some content" />

是否可以通过属性字段使Zend生成?

Is there a way to make Zend generates meta with the property field?

谢谢

推荐答案

听起来像您需要创建自己的视图帮助器,扩展标准Zend Framework HeadMeta视图帮助器并实现称为appendProperty()的方法一样appendName()的行为.

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().

由于appendName()方法似乎是在__call()方法中处理的,因此看起来您的扩展类可以简单地复制相同的__call()形式作为父类,而将preg_match()中使用的模式从以下位置更改:

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)$/'

'/^(?P<action>set|(pre|ap)pend|offsetSet)(?P<type>Name|HttpEquiv|Property)$/'

[[作为附带说明,可能值得向ZF跟踪器提出问题,建议将此regex模式从内联代码中拉出,并将其放置为类的受保护成员.这样,子类(如您的子类)就可以简单地声明一个新模式,而不必复制"太多父代码.但是在向他们建议之前,我必须仔细检查一下.]

[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...

更新日期:2010-12-17

我发现要使其工作还需要更多.您需要覆盖受保护的成员$_typeKeys和受保护的方法_normalizeType()来处理新的属性"类型.

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);
        }
    }
}

如前所述,如果将在Zend_View_Helper_HeadMeta::__call()中检入的preg_match()模式分解为受保护的成员(如$_callPattern之类),则长度可能会短得多.这样,扩展类就不必复制__call()方法的大部分内容.只需覆盖受保护的成员$_typeKeys$_callPattern并实现受保护的方法_normalizeType(),如上所示.

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.

这篇关于Zend框架:元属性集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆