drqn5418 2016-10-24 11:23
浏览 72
已采纳

ZF2 - 如何使用require_once作为$ this-> render?

In my ZF2 i am trying to use $this->render('render/email/template') but its failing to load the file, therefore when i do following method, it worked.

But while using require_once i cant get the content in $variable to process later, it dumps the value instantly, where ->render() method can put it in a variable.

How do i use require_once like ZF $this->render?

define('ROOT_PATH', dirname(__DIR__));
require_once (ROOT_PATH . '/Controller/render/email/template.phtml');

EDIT:

  public function indexAction() {
    error_reporting(E_ALL); ini_set('display_errors', 1);

    /*
    $this->layout('layout/email/header_footer');
    $view = new ViewModel(array(
        'name' => 'OKOKOKOK'
    ));  
    $view->setTemplate('layout/email/template');
    $html= $view;
    echo $html;*/

    define('ROOT_PATH', dirname(__DIR__));
    $this->layout('layout/email/header_footer');
    $view = new ViewModel(array(
      'name' => 'OKOKOKOK'
    ));     

    $view->setTemplate('layout/email/template');
    $viewRenderer = $this->getServiceLocator()->get('ViewRenderer');
    $html = $viewRenderer->render($view);
    echo $html;

    exit;
  • 写回答

2条回答 默认 最新

  • doulu1867 2016-10-24 11:32
    关注

    In ZF2 you need to set and render the template like this if you want to store it in a variable:

    $view->setTemplate(ROOT_PATH . '/Controller/render/email/template.phtml');
    $viewRenderer = $this->getServiceLocator()->get('ViewRenderer');
    $html = $viewRenderer->render($view);
    

    You can nest views as well:

    $mainView = new ViewModel();
    $mainView->setTemplate(ROOT_PATH . 'path/to/your/header_footer.phtml');
    
    $contentView = new ViewModel(array(
      'name' => 'OKOKOKOK'
    ));
    $contentView->setTemplate(ROOT_PATH . '/Controller/render/email/template.phtml');
    
    $mainView->addChild($contentView, 'content');
    

    You can find more information here

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作