title: Modify ViewModel after controller dispatch
body: <p>I'm trying to intercept the <code>ViewModel</code>, prior to it being rendered, and add it to another 'parent' view model - much like the way that the ZF2 layout wraps around the controllers returned content.</p>
  
  <p>The simple (working) way to do this would be in each controller action.</p>
  
  <pre><code>public function dashboardAction() { 
  
    $dashboardContent = new ViewModel(array('foo' =&gt; 'bar'));
  
    $parent = new ViewModel();
    $parent-&gt;setTemplate('foo/bar/parent-template'); 
  
    $parent-&gt;addChild($dashboardContent, 'content');
  
    return $parent;
  }
  </code></pre>
  
  <p>This works as expected and the 'child' view is correctly nested within the 'parent' in the  final output.</p>
  
  <p>As I have a number of controllers/actions that should all behave in the same way (resolved via their route name); I was hoping to encapsulate this in an event listener.</p>
  
  <pre><code>public function onBootstrap(MvcEvent $event)
  {
      $application  = $event-&gt;getApplication();
      $eventManager = $application-&gt;getEventManager();
  
      $eventManager-&gt;attach(MvcEvent::EVENT_DISPATCH, array($this, 'addUserAccountLayout'), -100);
  }
  
  public function addUserAccountLayout(EventInterface $event)
  {
      $routeMatch = $event-&gt;getRouteMatch();
      $controller = $event-&gt;getTarget();
      $result     = $event-&gt;getResult();
  
      if (! $result instanceof ViewModel || $result instanceof JsonModel) {
          return;
      }
  
      if (! $routeMatch instanceof RouteMatch || 0 !== strpos($routeMatch-&gt;getMatchedRouteName(), 'zfcuser') || $result-&gt;terminate()) {
          return;
      }
  
      $application    = $event-&gt;getApplication();
      $serviceManager = $application-&gt;getServiceManager();
      $accountService = $serviceManager-&gt;get('JobboardUser\Service\AccountService');
  
      $user = $accountService-&gt;getCurrentUser();
  
      $parent = new ViewModel();
      $parent-&gt;setVariables(compact('user'));
      $parent-&gt;setTemplate('jobboard-user/widget/user-account');
      $parent-&gt;addChild($result, 'content');
  
      $event-&gt;setResult($parent); 
  }
  </code></pre>
  
  <p>This however is not working; the normal view is rendered (without the parent). My guess is because I am either not using the correct event or event priority OR <code>$event-&gt;setResult($view)</code> is not the correct way to assign the result.</p>
  
  <p>How can I modify and then re-assign the view from within an event listener? </p>