Search code examples
phpcodeignitertemplatesviewregion

Within Codeigniter, how do I write a view file to a view file?


I have a thank_you.php file that simply contains the string "Thank you." formatted in a successful / green color.

I'm wanting to display this text on a specific region of the site, but not sure how to go about it. Within my main view file, I added the line:

<?php $this->template->add_region('success'); ?>

next to the "Submit" button.

Within my controller, I have a function that should, once called, display the view file "thank_you.php".

function greeter_thank_you() {
$this->template->write_view('success', 'thank_you.php');
}

I have the standard $alert, $content, $main, etc... regions within my Template file, but how do I echo this view file onto another view file? Is there some other way I need to go about it?


Solution

  • It seems you are using Collin Williams' CI Template library.

    As far as I know, you can't define a region inside another one, so it is better to send the value of the internal region (which you called it success) into the main region.

    • If you are using $this->template->write(); method to write content to the main region:

    Get the content of thank_you view file by using $this->load->view('thank_you', '', TRUE); and place it somewhere inside the second parameter of write().

    • If you are using $this->template->write_view() method:

    Use the following:

    $data = array(
        'foo'     => 'bar',
        'baz'     => 'qux',
        'success' => $this->load->view('thank_you', '', TRUE)
    );
    
    $this->template->write_view('main', 'main_region_view_file_here', $data/*, TRUE*/);
    

    And add the following inside the view file of the main region:

    if (! empty($success)) {
        echo $success;
    }
    

    I don't have any experience with Collin Williams' Template library, but I think this might do the trick.

    By the way, If you had any problem, I'm all ears!