2

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

我试过了

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

但我得到了

致命错误:Can't use method return value in write context in D:\Projects\Websites\php\ZendFramework\LearningZF\application\layouts\scripts\layout.phtml 第 5 行

附带说明一下,当我收到此错误时,为什么它没有显示在错误视图脚本中?错误显示在没有布局的空白页面中。

4

3 回答 3

3

有关致命错误的原因,请参阅问题PHP:无法在写入上下文中使用方法返回值

因此,您可以使用临时变量或$this->placeholder()->getRegistry()->containerExists("key")返回布尔值。

echo ($this->placeholder()->getRegistry()->containerExists("title")) ? " - " . $this->placeholder("title") : "";
于 2010-07-24T19:40:04.177 回答
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();

当然,通过简单地回显它来渲染。

于 2016-05-26T21:08:06.950 回答
0

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

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

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

根据我对 Benjamin Cremer 的回答的评论(如上所示的致命错误),我想出了一个很好的简单解决方案:

$content = $this->placeholder('placeholderName')->getValue();
if (!empty($content)) {
    echo $content;
}
于 2012-04-20T02:05:14.477 回答