2
votes

How can I check if a Zend View Placeholder isset before echo-ing it out? As I am wanting to prepend " - " to it before outputting it.

I tried

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

But I got

Fatal error: Can't use method return value in write context in D:\Projects\Websites\php\ZendFramework\LearningZF\application\layouts\scripts\layout.phtml on line 5

On a side note, how come when I got this error, why isn't it shown in the Error View Script? The error was shown in a blank page without layout.

3

3 Answers

3
votes

For the cause of the fatal error see the Question PHP : can’t use method return value in write context.

So you could either use a temporary variable or $this->placeholder()->getRegistry()->containerExists("key") which returns a boolean.

echo ($this->placeholder()->getRegistry()->containerExists("title")) ? " - " . $this->placeholder("title") : "";
1
votes

Another way to do this:

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

Then you may check if a placeholder exists with:

$registry->containerExists('myPlaceholder')

Or check the contents of your placeholder with:

$myPlaceholder->getValue();

And of course, render by simply echoing it.

0
votes

Warning: Missing argument 1 for Zend_View_Helper_Placeholder::placeholder() in /library/Zend/View/Helper/Placeholder.php on line 72

Notice: Undefined variable: name in /library/Zend/View/Helper/Placeholder.php on line 74

Fatal error: Call to undefined method Zend_View_Helper_Placeholder_Container::getRegistry() in /path/to/index.phtml on line 109

Per my comment to Benjamin Cremer's answer (fatal error shown above), I came up with a nice simple solution:

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