我希望将视图呈现给一个变量,而不直接将其发送到浏览器。我以前经常用蛋糕做。*。但是,我不知道如何在CakePHP3中做到这一点。你能告诉我怎么做吗?
发布于 2016-11-06 13:09:41
ViewBuilder是在CakePHP 3.1中引入的,它处理视图的呈现。当我想呈现给一个变量时,我总是去看看发送电子邮件是如何工作的。
从控制员那里:
function index() {
// you can have view variables.
$data = 'A view variable';
// create a builder (hint: new ViewBuilder() constructor works too)
$builder = $this->viewBuilder();
// configure as needed
$builder->layout('default');
$builder->template('index');
$builder->helpers(['Html']);
// create a view instance
$view = $builder->build(compact('data'));
// render to a variable
$output = $view->render();
}发布于 2021-03-21 11:27:18
对于Ajax请求/响应,我使用以下方法:
public function print(){
if ($this->request->is('ajax')) {
$data = $this->request->getData();
$builder = $this->viewBuilder()
->setTemplatePath('ControllerName')
->setTemplate('print');
->setLayout('ajax');
->enableAutoLayout(false);
$view = $builder->build(compact('data'));
$html = $view->render();
$res = ['html' => $html];
$this->set('response',$res);
$this->set("_serialize",'response');
}
}print.ctp在Template/ControllerName下面
https://stackoverflow.com/questions/40444618
复制相似问题