Welcome Guest, Not a member yet? Register   Sign In
Views sharing data
#1

[eluser]Mr. Pickle[/eluser]
In the CI documentation I find info on how to pass data to the view.
They don't speak of views sharing data.

What if I have for example this:

Code:
$data = array(
    'section' => $this->router->fetch_class(),
    'page_title' => 'This is the title',
    'content' => $this->site_model->get_content()
    'path' => $this->site_model->get_path()
);

Some parts I want to use in more than one view.
Of course I don't want to set a data array (with possibly lots of duplicate content) for all my views (this example is simplified)

I now have:

Code:
$this->load->view('header', $data);
   $this->load->view('navigation', $data);
   $this->load->view('subnavigation', $data);
   $this->load->view('content', $data);
   $this->load->view('footer', $data)

Should I do this? -> load the same data file for all my views? I'm asking this because besides the shared content there is also a lot of information in the data array that a view would never need to know.
#2

[eluser]LuckyFella73[/eluser]
Can you give a short example why you need the same data for different
views?

an other approach to organize your view(s)/page would be:
Code:
<?php
$data_header['title'] = 'This is the title';
$data['header'] = $this->load->view('header', $data_header, true);

$data_navigation['whatever'] = 'whatever';
$data['navigation'] = $this->load->view('navigation', $data_navigation, true);

$data_subnavigation['whatever'] = 'whatever';
$data['subnavigation'] = $this->load->view('subnavigation', $data_subnavigation, true);

$data_content['whatever'] = 'whatever';
$data['content'] = $this->load->view('content', $data_content, true);

$data_footer['whatever'] = 'whatever';
$data['footer'] = $this->load->view('footer', $data_footer, true);

$this->load->view('template', $data);
?>

Your template_view would contain the needed variables (echoed) at the desired place then.
#3

[eluser]Mr. Pickle[/eluser]
Well, if it is no problem to build a data array for each view, then I think that would work for me as well. I just wondered if there was a different solution.
I can have your solution or something likely:
Code:
$data_header = array(/* vars for header */);
    $this->load->view('header', $data_header);

    $data_navigation = array(/* vars for navigation */);
    $this->load->view('navigation', $data_navigation);

    $data = array(/* vars for content */);
    $this->load->view('content', $data);

    $data_footer = array(/* vars for navigation */);
    $this->load->view('footer', $data_footer);

But still some data can be shared by views. For example the username. I want to show this both in the header and the footer. Now what?
#4

[eluser]Mr. Pickle[/eluser]
Also I use one view for as a template and then include some sub-view files, so I usualy have the controller send one data array to the skeleton view files. I can maybe do something like this as well

Code:
$data = array(
    'header' => array(/* header vars */),
    'navigation' => array(/* navigation vars */),
    'content' => array(/* content vars */),
    'footer' => array(/* footer vars */),
);
$this->load->view('template', $data);

And then have the template

Code:
$this->load->view('header', $header);
$this->load->view('navigation', $navigation);
$this->load->view('content', $content);
$this->load->view('footer', $footer);

Why I use the template view file? -> I don't wan't to load the same view files at every controller function. Is something is added to the template I have to update all my controller functions! Now I only have to update the template view file. Got me thinking though, that if I want to have a section added, I always need to have data passed through the controller so I always have to update the controller, no matter what.

I'm very curious if you would work like this or call
Code:
$this->load->view('header', $header);
$this->load->view('navigation', $navigation);
$this->load->view('content', $content);
$this->load->view('footer', $footer);
at litteraly every controller function?
#5

[eluser]LuckyFella73[/eluser]
Quote:But still some data can be shared by views. For example the username.
I want to show this both in the header and the footer. Now what?

Then you could pass the username several times or just set a session value
that is available everywhere. I would prefer to set a session variable then.

About organizing the views there is no way you have to do. When I set up a
new website I first code the whole page in pure HTML and then seperate it
for my needs (simular like you do). There are countless ways how to put
all parts together. And it depends on where your content - and I mean HTML part -
is comming from or just personal preference. For small sites with mainly static
content I set up view files (skeleton like) for every page (displayed in your browser)
where all the dynamic parts like navi for example are echoed. If your content is
comming purely from a database you could set up one template file and echo the
content of course.




Theme © iAndrew 2016 - Forum software by © MyBB