3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Strategy
;
12 use Zend\EventManager\AbstractListenerAggregate
;
13 use Zend\EventManager\EventManagerInterface
;
14 use Zend\View\Renderer\PhpRenderer
;
15 use Zend\View\ViewEvent
;
17 class PhpRendererStrategy
extends AbstractListenerAggregate
20 * Placeholders that may hold content
24 protected $contentPlaceholders = array('article', 'content');
34 * @param PhpRenderer $renderer
36 public function __construct(PhpRenderer
$renderer)
38 $this->renderer
= $renderer;
42 * Retrieve the composed renderer
46 public function getRenderer()
48 return $this->renderer
;
52 * Set list of possible content placeholders
54 * @param array $contentPlaceholders
55 * @return PhpRendererStrategy
57 public function setContentPlaceholders(array $contentPlaceholders)
59 $this->contentPlaceholders
= $contentPlaceholders;
64 * Get list of possible content placeholders
68 public function getContentPlaceholders()
70 return $this->contentPlaceholders
;
76 public function attach(EventManagerInterface
$events, $priority = 1)
78 $this->listeners
[] = $events->attach(ViewEvent
::EVENT_RENDERER
, array($this, 'selectRenderer'), $priority);
79 $this->listeners
[] = $events->attach(ViewEvent
::EVENT_RESPONSE
, array($this, 'injectResponse'), $priority);
83 * Select the PhpRenderer; typically, this will be registered last or at
89 public function selectRenderer(ViewEvent
$e)
91 return $this->renderer
;
95 * Populate the response object from the View
97 * Populates the content of the response object from the view rendering
100 * @param ViewEvent $e
103 public function injectResponse(ViewEvent
$e)
105 $renderer = $e->getRenderer();
106 if ($renderer !== $this->renderer
) {
110 $result = $e->getResult();
111 $response = $e->getResponse();
114 // If content is empty, check common placeholders to determine if they are
115 // populated, and set the content from them.
116 if (empty($result)) {
117 $placeholders = $renderer->plugin('placeholder');
118 foreach ($this->contentPlaceholders
as $placeholder) {
119 if ($placeholders->containerExists($placeholder)) {
120 $result = (string) $placeholders->getContainer($placeholder);
125 $response->setContent($result);