2

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.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

3 Answers3

3

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") : "";
Community
  • 1
  • 1
Benjamin Cremer
  • 4,842
  • 1
  • 24
  • 30
  • This was a great idea in theory, but I got a Fatal Error with your solution.. :( plus I discovered a much simpler one. – Yes Barry Apr 20 '12 at 02:02
1

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.

Fractalizer
  • 91
  • 10
0

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;
}
Yes Barry
  • 9,514
  • 5
  • 50
  • 69