2012-06-19 39 views
2

我需要symfony 1.4 action,它將接收模板名稱列表作爲參數,並將這些呈現的模板作爲JSONed散列返回。代碼如下:Symfony 1.4。如何將模板呈現爲字符串

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

該代碼導致「該請求檢測到太多轉發」。拋出異常。我認爲這是因爲getPresentationFor創建內部請求到相同的模塊和操作。 所以問題是我怎樣才能實現我的目標,並獲得幾個模板呈現和返回? PS:我正在使用現有的系統,所以我不能使用partials或組件,只能使用模板。

回答

5

試試這個:

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

我個人擴展到sfAction包括getPresentation方法:

<?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(); 
    } 

} 
相關問題