/**
  * Populate the view model returned by the AcceptableViewModelSelector from the result
  *
  * If the result is a ViewModel, we "re-cast" it by copying over all
  * values/settings/etc from the original.
  *
  * If the result is an array, we pass those values as the view model variables.
  *
  * @param  array|ViewModel $result
  * @param  ViewModelInterface $viewModel
  * @param  MvcEvent $e
  */
 protected function populateViewModel($result, ViewModelInterface $viewModel, MvcEvent $e)
 {
     if ($result instanceof ViewModel) {
         // "Re-cast" content-negotiation view models to the view model type
         // selected by the AcceptableViewModelSelector
         $viewModel->setVariables($result->getVariables());
         $viewModel->setTemplate($result->getTemplate());
         $viewModel->setOptions($result->getOptions());
         $viewModel->setCaptureTo($result->captureTo());
         $viewModel->setTerminal($result->terminate());
         $viewModel->setAppend($result->isAppend());
         if ($result->hasChildren()) {
             foreach ($result->getChildren() as $child) {
                 $viewModel->addChild($child);
             }
         }
         $e->setResult($viewModel);
         return;
     }
     // At this point, the result is an array; use it to populate the view
     // model variables
     $viewModel->setVariables($result);
     $e->setResult($viewModel);
 }
 /**
  * Recursively search a view model and it's children for the given templateName
  *
  * @param ViewModel $viewModel
  * @param string $templateName
  * @return boolean
  */
 protected function searchTemplates($viewModel, $templateName)
 {
     if ($viewModel->getTemplate($templateName) == $templateName) {
         return true;
     }
     foreach ($viewModel->getChildren() as $child) {
         return $this->searchTemplates($child, $templateName);
     }
     return false;
 }