【问题标题】:Symfony 1.4. How to render template into stringSymfony 1.4。如何将模板渲染成字符串
【发布时间】:2012-06-21 08:56:27
【问题描述】:

我需要 symfony 1.4 操作,它将接收模板名称列表作为参数并将这些呈现的模板作为 JSON 散列返回。代码如下:

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

代码导致“检测到此请求的转发次数过多”。要抛出的异常。我认为这是因为 getPresentationFor 创建了对相同模块和操作的内部请求。 所以问题是我怎样才能实现我的目标并获得渲染和返回的几个模板?

PS:我正在使用现有系统,所以我不能使用部分或组件,只能使用模板。

【问题讨论】:

    标签: symfony1 symfony-1.4


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 2018-07-29
      • 2012-12-30
      • 2020-10-03
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多