title: Rendering issue on ZF2, trying to render layout to get html code
body: <p>I've an issue with PhpRenderer, let's start by the context. I have to generate a bunch of static pages to cache them (with cache storage using file system adapter). So I wired an event on  EVENT_ROUTE to serve the static page if it exists, and an event on EVENT_FINISH to get the response content and write it to a cache file. It works just fine.</p>
  
  <p>My problem is, I want to generate these pages before navigation (I've around 3 millions pages to generate). My idea was to create a controller I can call in CLI, to be able to schedule the huge task. So in my second controller, I call the one that return the ViewModel I'm interested in, build the renderer, and render my action ViewModel and my layout to get the whole HTML of my page.</p>
  
  <p>Here is the code :</p>
  
  <pre><code>public static function GetStaticResponse(ServiceManager $sm, AbstractActionController $controller, array $routeParams){
      $event = new MvcEvent();
      $request = new Request();
      // $controller = new $controller();
      $router = $sm-&gt;get('Router');
  
      // Test values!!!
      // $routeMatch = new RouteMatch(array('controller' =&gt; 'index', 'action' =&gt; 'showad', 'ad_id' =&gt; '1002293747'));
      // Test values end !!
  
      $routeMatch = new RouteMatch($routeParams);
  
      $event-&gt;setRouter($router);
      $event-&gt;setRouteMatch($routeMatch);
      $controller-&gt;setEvent($event);
      $pluginManager = $sm-&gt;get('ControllerPluginManager');
      $controller-&gt;setServiceLocator($sm);
      $controller-&gt;setPluginManager($pluginManager);
  
      $view = $controller-&gt;dispatch($request);
  
      $config = $sm-&gt;get('Config');
  
      $renderer = new PhpRenderer($config);
      $map = new TemplateMapResolver(
          $config['view_manager']['template_map']
      );
  
      $renderer-&gt;setResolver($map);
      $renderer-&gt;setHelperPluginManager($sm-&gt;get('ViewHelperManager'));
      $view-&gt;setTemplate('showad');
      $content = $renderer-&gt;render($view);
  
      $renderer = new PhpRenderer($config);
      $map = new TemplateMapResolver(
          $config['view_manager']['template_map']
      );
      $renderer-&gt;setResolver($map);
      $renderer-&gt;setHelperPluginManager($sm-&gt;get('ViewHelperManager'));
      $viewLayout = new ViewModel();
      $viewLayout-&gt;setTemplate('layout/layout');
      $viewLayout-&gt;setVariables(array("content" =&gt; $content));
  
      $render = $renderer-&gt;render($viewLayout);
      unset($event, $request, $router, $routeMatch, $controller, $pluginManager, $view, $config, $renderer, $content, $viewLayout);
      return $render;
  
  }
  </code></pre>
  
  <p>this method works fine when I only execute it once. When I call this method again in a loop, the Renderer seems to have some static properties and whenever the layout calls helpers like 'headTitle' or 'headLink', the title and link are append again and again.</p>
  
  <p>Ex : first loop</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;&lt;html lang="fr"&gt;
    &lt;head&gt;
      &lt;meta http-equiv="content-type" content="text/html;charset=UTF-8" /&gt;
      &lt;link rel="icon" type="image/ico" href="http://apte.local/images/favicon.ico"&gt;
              &lt;title&gt;title&lt;/title&gt;            
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;!-- Le styles --&gt;
      &lt;link href="http://apte.local/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon"&gt;
  &lt;link href="http://apte.local/css/style.min.css" media="screen" rel="stylesheet" type="text/css"&gt;
  &lt;link href="http://apte.local/js/jquery/css/ui-lightness/jquery-ui.css" media="screen" rel="stylesheet" type="text/css"&gt;
      &lt;!-- Scripts --&gt;
  </code></pre>
  
  <p>second loop :</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;&lt;html lang="fr"&gt;
    &lt;head&gt;
      &lt;meta http-equiv="content-type" content="text/html;charset=UTF-8" /&gt;
      &lt;link rel="icon" type="image/ico" href="http://apte.local/images/favicon.ico"&gt;
              &lt;title&gt;title - title&lt;/title&gt;&lt;!-- title generated with headTitle --&gt;            
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;!-- in layout headMeta is used to generate meta --&gt;
      &lt;!-- Le styles --&gt;
      &lt;link href="http://apte.local/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon"&gt;
  &lt;link href="http://apte.local/css/style.min.css" media="screen" rel="stylesheet" type="text/css"&gt;
  &lt;link href="http://apte.local/js/jquery/css/ui-lightness/jquery-ui.css" media="screen" rel="stylesheet" type="text/css"&gt;
  &lt;link href="http://apte.local/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon"&gt; &lt;!-- in layout headLink helper is used to generate the link --&gt;
      &lt;!-- Scripts --&gt;
  </code></pre>
  
  <p>I don't know if my idea is right, I thought that using the same logic as unit testing an action could do the job, am I missing something ?</p>
  
  <p>I wonder if there is a way to trigger the events I already coded and that are working fine in http mode.</p>
  
  <p>(I've tested to wire an echo 'test' on EVENT_DISPATCH but my event is fired only once, the $controller->dispatch(...) doesn't fire the dispatch event of the current event manager.)</p>
  
  <p>If someone has an idea, or has faced a similar case, I would be glad to read how you've done to solve that.</p>