Zend framework 显示捕获的占位符视图帮助器内容后自动清除该内容

Zend framework 显示捕获的占位符视图帮助器内容后自动清除该内容,zend-framework,zend-view,Zend Framework,Zend View,在分部中生成邮件消息时,我使用占位符视图帮助器,如下所示: <?php $this->placeholder('mykey')->startCapture() ?> Some content here that is actually more complicated than just text. Trust me that, in this case the, using the placeholder for capturing is d

在分部中生成邮件消息时,我使用占位符视图帮助器,如下所示:

<?php $this->placeholder('mykey')->startCapture() ?>
    Some content here that is actually more complicated than just text. 
    Trust me that, in this case the, using the placeholder for capturing 
    is desirable.
<?php 
    $this->placeholder('mykey')->endCapture();
    echo $this->placeholder('mykey');
 ?>

这里的一些内容实际上比文本更复杂。
相信我,在本例中,使用占位符捕获
这是可取的。
问题是,如果我在同一请求中的不同邮件消息的不同部分中使用相同的密钥,那么捕获的内容仍然存储在该密钥的容器中。原则上,我希望partials可以自由地使用他们想要的任何占位符键,而不必担心其他partials正在使用什么

我知道我可以在不同的部分使用不同的键。或者,我可以在使用/显示后手动清除内容,方法如下:

$this->占位符('mykey')->set(''

但我不想把所有这些负担都放在使用占位符的视图脚本上

我想我要做的是创建自己的自定义占位符视图帮助器,在输出内容后自动清除捕获的内容

我尝试过创建一个自定义占位符容器(扩展
独立的
容器,覆盖
toString()
方法),创建一个自定义视图帮助器(扩展标准的
占位符
视图帮助器),并告诉视图帮助器使用自定义容器类

但我不断地遇到一些与丢失的视图对象相关的错误。显然,我遗漏了一些关于视图对象、容器和注册表如何交互的信息,甚至可能是插件系统如何加载它们的信息


非常感谢您的建议和一般性解释。

您需要在
占位符
查看帮助程序中设置此容器,否则
Zend\u查看帮助程序占位符\u注册表将自动加载
Zend\u查看帮助程序\u占位符\u容器
。因此,首先需要手动设置自定义容器。在视图脚本中:

$this->getHelper('placeholder')
     ->getRegistry()
     ->setContainerClass('My_View_Helper_Placeholder_Container');
或者以Bootstrap.php中的_initCustomContainer()为例:

$view = $this->bootstrap('view')->getResource('view');
$view->getHelper('placeholder')
     ->getRegistry()
     ->setContainerClass('My_View_Helper_Placeholder_Container');
然后,您需要基于
Zend\u View\u Helper\u Placeholder\u Container
(而不是
Zend\u View\u Helper\u Placeholder\u Container\u Standalone
)创建此类。我建议您保持选项打开以重置内容或不重置内容,您可以使用setter执行此操作:

class My_View_Helper_Placeholder_Container
    extends Zend_View_Helper_Placeholder_Container
{
    protected $_resetCapture = true; // defaults true for new behaviour

    public function setResetCapture($flag)
    {
        $this->_resetCapture = (bool) $flag;
        return $this;
    }

    public function toString($indent = null)
    {
        $return = parent::toString($indent);

        if ($this->_resetCapture) {
            $this->exchangeArray(array());
        }

        return $return;
    }
}
默认情况下,重置捕获已打开,但要关闭它并开始捕获,请执行以下操作:

$this->placeholder('my_key')->setResetCapture(false)->startCapture();
要再次打开它,请执行以下操作:

$this->placeholder('my_key')->setResetCapture(true);
在视图脚本中,使用:

$this->占位符('mykey')->captureStart('SET');

或使用类常量:


$this->placeholder('mykey')->captureStart(Zend_View_Helper_placeholder_Container_Abstract::SET);

谢谢,@Jurian。我做了很多工作,但你的更清晰、更灵活。让我仔细看看,我会尽快回复你的。再次感谢!;-)工作起来很有魅力(只需一两次小的编辑)。谢谢你的解释。;-)