只渲染指定视图 - CakePHP
我想单独渲染视图,我想避免渲染 和 default.ctp 文件中定义的其他部分。
我有想要在元素中呈现的信息:
<?php
echo $post['Post']['id'];
?>
我怎样才能做到这一点?它用于 ajax 响应。
谢谢!
I would like to render the view by itself, I want to avoid rendering the <head>
and other sections defined in the default.ctp file.
I have the info that I want to render in an element:
<?php
echo $post['Post']['id'];
?>
How can I accomplish this? It is for an ajax response.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个新布局(例如 /app/views/layouts/ajax.ctp),其中包含:
然后在您的控制器中,在您正在使用的操作中,定义布局:
参考: http://book.cakephp.org/view/962/Page-lated-Attribute-layout
You can create a new layout (/app/views/layouts/ajax.ctp, for example) that contains:
Then in your controller, in the action you are using, define the layout:
Reference: http://book.cakephp.org/view/962/Page-related-Attribute-layout
只需创建一个名为
ajax.ctp
的布局,该布局仅在控制器中包含 then
如果您想以编程方式对所有 AJAX 请求执行此操作,只需将
RequestHandler
组件添加到您的控制器即可并创建一个
beforeRender
方法,例如Just create a layout called
ajax.ctp
which only containsthen in the controller put
If you want to do this programmatically for all AJAX requests, just add the
RequestHandler
component to your controllerand create a
beforeRender
method like