2
votes

I need symfony 1.4 action which will receive list of template names as parameter and return these rendered templates as JSONed hash. Here is the code:

foreach ($templateNames as $templateName)
  $result[$templateName] = $this->getController()->getPresentationFor($this->getModuleName(), $this->getActionName(), $templateName);

The code causes "Too many forwards have been detected for this request." exception to be thrown. I assume that this is because getPresentationFor creates internal request to same module and action. So the question is how can I achieve my goal and get several templates rendered and returned?

PS: I am working with existing system so I cannot use partials or components but templates only.

1

1 Answers

5
votes

Try this:

$view = $this->getController()->getView($this->getModuleName(), $this->getActionName(), sfView::SUCCESS);
$view->execute();
$view->getAttributeHolder()->add($this->getVarHolder()->getAll());
$result[$templateName] = $view->render();

I personally extended sfAction to include a getPresentation method:

<?php

abstract class kfAction extends sfAction {

    public function getPresentation($viewName = sfView::SUCCESS) {
        $view = $this->getController()->getView($this->getContext()->getModuleName(), $this->getContext()->getActionName(), $viewName);
        $view->execute();
        $view->getAttributeHolder()->add($this->getVarHolder()->getAll());
        return $view->render();
    }

}