Note the upgraded forum! If you are experiencing issues logging in, you may need to reset your password which should send an email. If the email doesn't arrive, be sure to check your spam folder just in case.

How would one use ajax ? opt-in controller, or custom controller

edited September 2011 in Share
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.


  • edited 1:14AM
    You could create a page and associate it to a layout that doesn't include the header and footer. Layouts can be found in the fuel/applications/views/_layouts folder.
  • edited 1:14AM
    Thank you for your reply, but if i did that, what about degredation ? how would i pass the layout variable if its an ajax request and if its not an ajax request (mean while i am still not sure if i should create controllers or i can still use the opt-in controller) ?

    Thank s
  • edited 1:14AM
    Ok sir, what i did was i created another view file and placed
    <?=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.
  • edited 1:14AM
    Sure. You can pass variables to the page using the opt-in controller but you must specify the view to associate with the page since the URI will contain additional segments of variable information and will not have a view file to match (e.g. about/my_ajax_page/variable1/variable2):

    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.
  • edited 1:14AM
    I am really greateful for you taking your time to respond. Do you have a donate button or something so i can give back.

    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
  • edited 1:14AM
    basically what i have detected, is that none of my layout variable overrides are working, for cms pages.

    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
  • edited 1:14AM
    Ahh yes... that is because page variables set in the admin override any other variables and the layout variable is one of them.

    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
  • edited 1:14AM
    Thank you so much, that worked.

    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');}?>
Sign In or Register to comment.