Zend 框架:元属性集成

发布于 2024-10-08 12:28:30 字数 469 浏览 2 评论 0原文

我正在尝试根据页面内容将一些元(采用以下格式)添加到页面的头部:

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

像这样使用 headMeta()->appendName

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

在header:

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

有没有办法让 Zend 生成带有 property 字段的 meta

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2024-10-15 12:28:30

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

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

'/^(?Pset|(pre|ap)pend|offsetSet)(? PName|HttpEquiv)$/'

'/^(?Pset|(pre|ap)pend|offsetSet)(?PName|HttpEquiv|Property )$/'

[作为旁注,可能值得向 ZF 跟踪器提出问题,建议从内联代码中提取此正则表达式模式,并将其放置为类的受保护成员。这样,子类(例如您的子类)可以简单地声明一个新模式,而不是“复制”如此多的父代码。但在我向他们提出建议之前,我必须先进行更多的观察和测试。]

无论如何,只是在黑暗中进行尝试......

更新:2010-12-17

我发现了一点需要更多才能使其发挥作用。您需要重写受保护的成员 $_typeKeys 和受保护的方法 _normalizeType() 来处理新的“Property”类型。

您的扩展类可能如下所示:

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(),如上所示。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文