• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

I've been playing around with a template tutorial, one of the key steps in which is to grab the contents of a view and make it equal to a variable.

So say I have a view called contact_us.php.

Then in the controller I have a function to load the template including this:

&data;['content'] = 'contact_us';

To my amazement this works and the contents of contact_us.php view is loaded into $content.

My question is, how does Codeigniter know that I want to load the contents of that file and not the text contact_us ?

[eluser]Eric Barnes[/eluser]
From that code snippet I have no idea. Smile Probably doing something with the data array in the actual template library.

I suspect if you look in the template files themselves, you will find something like:


you are just loading the $content variable with your code.

No, I can assure you I'm starting with a clean install of CI and following a tutorial by Jeffrey Way of Nettuts Plus -


- if you're interested.

It's very simple. I have a controller called site.php with a function:

function contact_us()

I also have views called template.php and contact_us.php.The template view includes the statement:


if I go to the url http;//localhost/sitefolder/index.php/site/contact_us I load the template view as you would expect and the data from $content is displayed as you would expect, the data having been passed to the template view.

What's puzzling me is how CI knows that the value 'contact_us' refers to the contents of the Contact_us view. It works in Jeffrey Way's tutorial and it works on my computer but why?

Oh, THAT part?

That's magic.

(Go back and read my post again, and refresh your knowledge of loading views: http://ellislab.com/codeigniter/user-gui...views.html)

I don't think you're getting my point. I understand perfectly how a view is loaded with the $data array. My question is about the population of the $data array. It's the line:


that puzzles me. What it's actually doing is loading the contents of the file contact_us.php into the array, so the key is 'content' and the value is 'whatever is in the file'.

How does CI know that I didn't want the value to be the text contact_us?

Does it first do a search for matching filenames and only then, if no matches, assume I mean text?

no it isn't.

it is loading the word "contact_us" into the array element $data['content']

it is passing the word "contact_us" into the template.php file as the variable $content

it is passing that into $this->load->view($content), which is the same as saying $this->load->view("contact_us")

Therefore, it is loading view "contact_us"

If you are looking to load the contents of the view into a variable, use the 3rd parameter set to TRUE:

$content = $this->load->view($content,'',true);

I'm slow - but I get there in the end. Yes of course I see now. Many thanks for taking the time to lead me by the hand.

Now I'm going away to find a dark place to curl up...

Sorry about this but I've come out of my dark place and I realise you still haven't answered my original question.

We don't have a situation of $this->load->view($content) as in your post. We have a situation of $this->load->view('template', $data) where $data contains a key value pair of 'content'=>'contact_us'.

When a view is loaded, CI turns the data array keys into variables that can then be used in the view. If I wanted to pass in the text 'page_title' I would do $data['title'] = 'page_title'. Then in the view I would echo $page_title and get back my text.

However in this case, CI interprets the words 'contact_us' as the contents of a file with that filename. I still don't get it.

something like this should work:

function contact_us()
  $data = array( 'content' => $this->load->view('contact_us', '', TRUE) /* load the view 'contact_us.php' into a local variable */ );
  $this->load->view('template', $data);

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.