douqie1852 2011-06-14 10:55
浏览 17
已采纳

Zend重用视图

For every new controller and action I create, Zend expects a template file in /views/scripts/controllername. However, I would like to have multiple actions share a single template into which text from a database can be injected.

I currently use a layout and delegate page specific views with echo $this->layout()->content. I tried the following:

class SomeController extends Zend_Controller_Action{

    public function someAction() {
        $path = $this->view->getScriptPath();
        $this->view = new Zend_View();
        $this->view->setScriptPath($path);
        $this->view->render('default.phtml');
    }
}

However, I get an error that the script 'some/some.phtml' not found in path. How do I do this correctly?

  • 写回答

2条回答 默认 最新

  • dseve40868 2011-06-14 11:05
    关注

    You have to use 'default' (without the extension) and call the method directly (not on the view), e.g.

    $this->render('default');
    

    See Zend_Controller_Action::render

    render( string|null $action = null, string|null $name = null, bool $noController = false ) : void

    Render a view

    Renders a view. By default, views are found in the view script path as /.phtml. You may change the script suffix by resetting {@link $viewSuffix}. You may omit the controller directory prefix by specifying boolean true for $noController.

    By default, the rendered contents are appended to the response. You may specify the named body content segment to set by specifying a $name.


    If you want to provide a specific script, use

    $this->renderScript('controller/action.phtml');
    

    See Zend_Controller_Action::renderScript

    renderScript( string $script, string $name = null ) : void

    Render a given view script

    Similar to {@link render()}, this method renders a view script. Unlike render(), however, it does not autodetermine the view script via {@link getViewScript()}, but instead renders the script passed to it. Use this if you know the exact view script name and path you wish to use, or if using paths that do not conform to the spec defined with getViewScript().

    By default, the rendered contents are appended to the response. You may specify the named body content segment to set by specifying a $name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?