Zend Framework:检查是否存在Zend View占位符

时间:2010-07-21 15:04:38

标签: php zend-framework zend-view

如何在回显之前检查Zend View占位符是否已设置?因为我想在输出之前将“ - ”添加到它之前。

我试过

echo isset($this->placeholder('title')) 
    ? ' - ' . $this->placeholder('title') 
    : '';

但我得到了

  

致命错误:无法使用方法返回   写入上下文中的值   d:\项目\网站\ PHP \ ZendFramework \ LearningZF \程序\布局\脚本\ layout.phtml   在第5行

另一方面,为什么我收到此错误,为什么错误视图脚本中没有显示?错误显示在没有布局的空白页面中。

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

另一种方法:

// get a placeholder registry instance and create a container
$registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
$myPlaceholder = $registry->createContainer('myPlaceholder');

然后您可以检查占位符是否存在:

$registry->containerExists('myPlaceholder')

或者使用以下方法检查占位符的内容:

$myPlaceholder->getValue();

当然,通过简单回声来渲染。

答案 2 :(得分:0)

  

警告:在第72行的/library/Zend/View/Helper/Placeholder.php中缺少Zend_View_Helper_Placeholder :: placeholder()的参数1

     

注意:未定义的变量:第74行的/library/Zend/View/Helper/Placeholder.php中的名称

     

致命错误:在第109行的/path/to/index.phtml中调用未定义的方法Zend_View_Helper_Placeholder_Container :: getRegistry()

根据我对Benjamin Cremer的回答(上面显示的致命错误),我提出了一个很好的简单解决方案:

$content = $this->placeholder('placeholderName')->getValue();
if (!empty($content)) {
    echo $content;
}