YumYumYum YumYumYum - 1 month ago 11
PHP Question

ZF2 - how to use require_once as $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;

Answer

In ZF2 you need to set and render the template like this:

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