【发布时间】:2016-06-06 14:25:15
【问题描述】:
我需要在 phalcon 中加载一部分视图,以便在 ajax 调用中返回。为此,我需要在变量中设置视图。现在发生的事情是我正在获取所有模板,包括页眉和页脚。
【问题讨论】:
我需要在 phalcon 中加载一部分视图,以便在 ajax 调用中返回。为此,我需要在变量中设置视图。现在发生的事情是我正在获取所有模板,包括页眉和页脚。
【问题讨论】:
您需要禁用其余的视图渲染。确切情况示例(为 AJAX 返回 JSON 响应)
$viewParams = [
'param1' => '....',
'param2' => '....',
];
$response = new \stdClass();
// Render view into a variable
$this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_NO_RENDER);
$response->html = $this->view->getRender('yourTemplateDir', 'yourTemplateName', $viewParams, function($view) {
$view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
});
// Disable view
$this->view->disable();
return $this->response->setJsonContent($response);
【讨论】:
$this->view->disable();这一行可以跳过当return->使用setJsonContent时。