CakePHP $ this-> layout = 'ajax'

my css dosnt seems to work when I declare $ this-> layout = 'ajax' in my controller.

in my view1.ctp, I created a dialog using jquery. content will get from view2.ctp via jquery.ajax (). I created the html, id classes and all similar to view1.ctp.

im content gets ok. the problem is that id and classes i declared in view2.ctp do not work. In short, I get an empty dialog.

any idea on how to solve this? thank you in advance. Hooray!

in my view1.ctp:

<div id="thisDialog"> <div id="content"> </div> </div> <script> jQuery('.test_box').live('click',function(){ jQuery('#content').empty(); jQuery.ajax({ async:false, url: '/controller1/view2/'+id //url pointing to my 1controller.php success:function(data){ jQuery('#content').append(data); } }); jQuery('#thisDialog').dialog('open'); }); </script> 

here is controller1.php

 function view2($id) { $this->layout = 'ajax'; $query = $this->Model->find('all') //somequery here } 

here view2.ctp

 <div class='box'> <div class='title'> <h5>Title here</h5> </div> <div class='contents'> <table> <tr> <td class='head'></td> <td class='head'></td> </tr> <tr> <td class='alt'>Field content1 here</td> <td>Fields content2 here</td> </tr> </table> </div> </div> 
+4
source share
3 answers

An AJAX layout is a simple empty file that contains only the piece of HTML (or other content) that you display. It does not contain styles, because style information must come from the page where you paste the content. Defining your styles for the id and classes on the calling page will solve your problem.

+4
source

The 'ajax' layout can be found in cake / libs / view / layouts / ajax.ctp, and this content is:

 <?php echo $content_for_layout; ?> 

This essentially only outputs the view without any surrounding HTML (therefore CSS or JS).

0
source

Why do you need CSS for the 'ajax' layout? Anyway:

  • The layout can be found at /cake/libs/view/layout/ajax.ctp (as said above, without the usual html files, just an echo to output data from the controller.
  • You can copy this file (or default.ctp) to /app/views/layouts/ajax.ctp so that it is used instead of the file located in the core files of the cake.
0
source

All Articles