Welcome Guest, Not a member yet? Register   Sign In
Templates
#1

[eluser]Perkin5[/eluser]
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:

Code:
&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 ?
#2

[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.
#3

[eluser]jmadsen[/eluser]
I suspect if you look in the template files themselves, you will find something like:

$this->load->view($content);

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

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

http://www.youtube.com/watch?v=gvGymDhY49E

- if you're interested.

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

Code:
function contact_us()
    {
        $data['content']='contact_us';
        $this->load->view('template',$data);
    }

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

Code:
$this->load->view($content);

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?
#5

[eluser]jmadsen[/eluser]
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)
#6

[eluser]Perkin5[/eluser]
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:

Code:
$data['content']='contact_us';

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?
#7

[eluser]jmadsen[/eluser]
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);
#8

[eluser]Perkin5[/eluser]
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...
#9

[eluser]Perkin5[/eluser]
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.
#10

[eluser]Rolly1971[/eluser]
something like this should work:

Code:
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);
}




Theme © iAndrew 2016 - Forum software by © MyBB