ed209
ed209

Reputation: 838

Codeigniter equivalent of include

I am working on a codeigniter project that requires me to display different html in a form depending on the situation.

All I really want to do is pass this information to the view and have it output on the screen.

It is important to note that I use a modified version of codeigniter that doesn't require any sort of $this->load->view in order to create a view. The header and footer are set automatically, so using $this->load->view likely isn't going to work.

I just want something that finds my file and allows it to pass it to my view so I can output it. I know I can do this with include, but was wondering if there is a simpler way.

Upvotes: 0

Views: 202

Answers (4)

ed209
ed209

Reputation: 838

I figured it out. I probably shouldn't have stated that it was modified because other than the fact that you can't use load->view it was irrelevant. As stated I just wanted to output the file as view data. What I came up with is below.

public function form($form_name = NULL)
{
    $this->view_data['form'] = $this->load->file(APPPATH.'views/stone_sheets/rectangular.php',TRUE);
}

Basically my problem was that I didn't know there was a load file helper and also when I tried to use it I didn't have APPPATH at the beginning. Thanks for the help.

Upvotes: 1

dasper
dasper

Reputation: 722

Sounds like this feature of not requiring to load a view is actually going to hinder you. I would not recommend this practice. You could have it infer that if you do not pass a view it pulls a default view but then you can override this. You can create a MY_Controller with a method to handle your template and views and it will assume if you do not explicitly state a view to call a view in the dir the same name as the class and the file the same name as the method in the uri (http://ellislab.com/codeigniter/user-guide/general/routing.html). This way if you ever need to explicitly call a view or alter anything, you just override the method or fill a property:

if (count($this->loadViews))
{
    foreach ($this->loadViews as $view)
        $this->load->view($view);
}else
{
    $this->load->view($this->uri->segment(1, 'welcome') . '/' . $this->uri->segment(2, 'index'))
}

You can also call views within views that may still work so you can always go into the default view that is being called by your modified version of CI and do something like

<? if isset($flag): ?>
    $this->load->view('partial_view1');
<? else ?>
    $this->load->view('partial_view2');
<? endif; ?>

You can also look into hooks to take care of this as well. I had one project where my views and templates were controlled by hooking in and ob_start the views, then scrubbing them, then outputting the data. http://ellislab.com/codeigniter/user-guide/general/hooks.html

I hope either of these solutions help. If you need anything else just let me knwo.

Upvotes: 1

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

I am not sure what you modified in the $this->load->view() method. Either way you can pass a variable to your view and based on that variable you can include the respective file inside your main view

For Example assume you have passed $situation variable to the view than do this

if($situation == 'foo')
{
   include(APPPATH.'views/file1.php');
}
else
{
   include(APPPATH.'views/file2.php');
}

Upvotes: 0

azngunit81
azngunit81

Reputation: 1604

You need to give us more info since you are using a non-standard codeigniter we are not sure where are you being stump on the code since its different from standard.

Normally you need to to pass some data to do some logic OR (best practice) is to have your view controller switch for your form inside your controller and have the standard view first and then a separate form depending on situation.

ex:

public function some_controller($situation)
{
    $this->load->view('head');
    switch($situation)
    {
       case '1': $this->load->view('view1');
        case '2': $this->load->view('view2');
     }
     $this->load->view('footer');
}

However since we don't know what is this "non-standard" codeigniter it will be not possible for us to troubleshoot what got ommited.

Upvotes: 0

Related Questions