How would one use ajax ? opt-in controller, or custom controller
Hello, Im still kind of new to Ci and newer to Fuel cms. I am wondering the right way to use ajax to load in views.
For example i have a page with about 4 links to other areas in my site, but instead of having the pages refresh, when going to the different links, I would like to use jquery ajax to retrieve the different pages/views and load them into a particular div. And thats without the header and the footer.
Please how would i do that ?
do i need to create custom controllers for all my pages ?
And would i still be able to add them to the cms, so editors can still make changes to the pages ?
thank you in advance.
Comments
Thank s
<?=fuel_set_var('layout', '')?>
this worked flawlessly.
But leads me to anew problem, so do i have to duplicate all my view files just for ajax requests ?
is there a way to pass in a view layout variable based on type of request i.e if its an ajax request, or just a normal request , so that i dont have to duplicate my view files ?
thanks again.
http://www.getfuelcms.com/user_guide/general/creating-pages
For example, if you have a page located at "about/my_ajax_page.php" you can create a fuel/application/views/_variables/about.php variables file which specifies which view to use for those URI locations in which you use additional URI segments to pass variable information to your page. The following means "set the view for about/my_ajax_page and any URI locations starting with that":
$pages['about/my_ajax_page$|about/my_ajax_page/:any'] = array('view' => 'my_ajax_page');
Alternatively, you could put it in the global.php variables file, but that will get loaded for every page and may not be necessary to do that
Also, you can use the is_ajax() function (in the ajax_helper) in your view file to set the layout variable to be empty like you did above.
Let me know if that answers your question.
This is both intresting and furstrating. I decided to go with the ajax helper, and this is what i did.
I first of all tested to see if it works by putting this
<?php if(is_ajax()){
fuel_set_var('layout', '');
}
?>
in one of my view files. (Static view file). and it worked flawlessly.
so i then went into my global config.php file and put this in
if(is_ajax()){$vars['layout'] = '';}
else {
$vars['layout'] = 'main';
}
But for some reason, when i browse pages that were made in the cms without a physical view file, it still loads the main layout.
I had my contact page working too, but all of a sudden, it too stopped. i have even gone into the controller, in the index function and put in
if(is_ajax()){$vars['layout'] = '';}
and its still loading the main layout i.e page with the header and footer blocks
the only page that seems to be working is the one with a view file.
thanks again in advance
and even for one of my controller pages even though i have declared the layout override in its view file and variable file.
thanks again
Perhaps in your main layout use the is_ajax() function to change how the layout renders. Or you can create a new layout file and then add it to the fuel/application/config/MY_fuel_layouts.php
for those trying to do the same thing, this is what i did
<?php if(is_ajax()){
echo fuel_var('body', '');
}
else {
?>
<?php $this->load->view('_blocks/header')?>
<?php echo fuel_var('body', ''); ?>
<?php $this->load->view('_blocks/footer');}?>