我正在编写API。 试图渲染一些模板, 例如index / index
配置:
'index/index' => __DIR__ . '/../view/layout/index.phtml',
index.phtml:
<span><?= $this->var ?></span>
我需要一个字符串作为结果发送到dompdf来创建一个pdf文件,而不是在浏览器中显示
代码:
$renderer = new PhpRenderer();
$vm = new ViewModel();
$resolver = new TemplateMapResolver();
$resolver->setMap($this->templateMap);
$renderer->setResolver($resolver);
$template = 'index/index';
$vm->setTemplate($template);
$vm->setVariables($var);
$content = $renderer->render($vm);
不要生成它,也不要显示任何错误...
我是朝着正确的吗?
我的错误在哪里?
答案 0 :(得分:0)
视图/布局/ index.phtml:
<?php echo $this->key ?>
<?php echo $this->value ?>
动作控制器:
$view = new \Zend\View\Renderer\PhpRenderer();
$resolver = new \Zend\View\Resolver\TemplateMapResolver();
$resolver->setMap(array(
'mailTemplate' => __DIR__ . '/../../../../view/layout/index.phtml'
));
$view->setResolver($resolver);
$viewModel = new \Zend\View\Model\ViewModel();
$viewModel->setTemplate('mailTemplate')
->setVariables(array(
'key' => '1111111111111',
'value' => '2222222222222'
));
$result = $view->render($viewModel);
echo $result;