Part of the EllisLab Network
   
 
Sharing data between view partials
Posted: 15 October 2008 10:53 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-19-2007

Hi,

When I build an app, I usually create a master view like this :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<
html >
<
head>
<
title><?php echo title; ?></title>  
<?php echo $view_css; ?>
</head>
<
body>
<
div id="header">
<
h1><?php echo $title; ?></h1>
</
div>
<
div id="content">
<?php echo $content; ?>
</div>
<
div id="sidebar">
<?php echo $sidebar; ?>
</div>
<
div id="footer">
&
copy; mygreatwebsite.com
</div>
</
body>
</
html>

In my controller I make something like this :

class Welcome extends Controller
{
  
function Welcome()
  
{
    parent
::Controller();    
  
}    
  
function index()
  
{
    $data
= array(
      
'title'=>'My Great Title',
      
'content'=>$this->load->view('content_partial_view', '', TRUE),
      
'sidebar'=>$this->load->view('sidebar_partial_view', '', TRUE)
    );
    
$this->load->view('master', $data);
  
}
}

In my actual case, I have access to $title only in my master view but not in my content and sidebar partials. What if I want to have access to $title in my content view ? I need to make another array with the same title data specificaly for the content view. And the same goes for the sidebar view.

So I’m wondering if it’s possible to have a global $data accessible for all my views (master, content and sidebar).

(I used to make partial with the great View Library by coolfactor before, but there’s not update since July 2007, so I try to use the default CI View Library now raspberry)

Thanks for any helpful information smile

 Signature 

Mac OS X 10.4.11, Apache 1.3.33, PHP 5.2.2, CodeIgniter 1.7

Profile
 
 
Posted: 15 October 2008 11:31 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  18
Joined  10-15-2008

I use a slightly different approach. I abstract my view loading into a custom library called Page.

This lib has functions for adding and removing style sheets and javascripts and a few other things. It then has a function called render_page() which loads all the various views required

function render_page($view,$data) {
  $this
->CI->load->view('_header',$data);
  
$this->CI->load->view($view,$data);
  
$this->CI->load->view('_footer',$data);
}

you can then call this from your controller like so:

$data['docTitle'] = 'My document title';
$data['content'] = 'Page content';
$data['something'] = 'else';
$this->page->render_page('my_view_file',$data);

This keeps all $data in one array and has lots of scope for handling other common tasks (I use it in conjunction with a navigation generator for example).

Hope that helps

Profile
 
 
Posted: 16 October 2008 03:05 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-19-2007

Your approach is like the View Library I used before and it works pretty well, so I think I’m going to not change my habits and stay with it. Maybe in CI 2.0 I will have a view library I want to use raspberry

 Signature 

Mac OS X 10.4.11, Apache 1.3.33, PHP 5.2.2, CodeIgniter 1.7

Profile
 
 
Posted: 16 October 2008 03:11 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  633
Joined  12-26-2006

Hi,

>>> When I build an app, I usually create a master view like this :
 
Try this instead

class Welcome extends Controller
{
  
function Welcome()
  
{
    parent
::Controller();    
  
}    
  
function index()
  
{
    $data
= array();
    
$data['title'] = 'My Great Title';

    
$data[''content'] = $this->load->view('content_partial_view', $data, TRUE);
    $data['
sidebar']  = $this->load->view('sidebar_partial_view', $data, TRUE);

    // See Xwero'
s better way of using array data below
    $output
= $this->load->view('master', $data, TRUE);
    echo
$output;

  
}//endfunc

}//endclass

 
  

 Signature 

Joke of the day     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 16 October 2008 03:19 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

If you use load->vars the title will be ‘global’

$this->load->vars(array('title'=>'My Great Title'));
$data = array(
      
'content'=>$this->load->view('content_partial_view', '', TRUE),
      
'sidebar'=>$this->load->view('sidebar_partial_view', '', TRUE)
    );
$this->load->view('master', $data);

But you have to be careful not to overwrite variables when adding partial related data.

Profile
 
 
Posted: 16 October 2008 03:23 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

John a tip : don’t give all your arrays the same name. In the example you show the title key will be passed on to the master view too which is not needed.

load->vars does the same thing, added for the people who pay attention wink

Profile
 
 
Posted: 16 October 2008 03:33 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  31
Joined  07-19-2007

@John_Betong : Great tips, didn’t think about that smile It work well with my exemple but with a more complex controller it might be tricky.

@xwero : I never used the load->vars method before, but it’s exactly the method I need for what I want to do.

Thanks !

 Signature 

Mac OS X 10.4.11, Apache 1.3.33, PHP 5.2.2, CodeIgniter 1.7

Profile
 
 
Posted: 16 October 2008 03:54 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  633
Joined  12-26-2006

 
 
Hi @Xwreo and @Isuka,
 
I have amended my original post and removed $data where it was unnecessary.
 
 
@Isuka in complex controllers it does get tricky, just ensure all your variables are declared before loading the partial views otherwise some weird errorrs appear which are not easy to track down.
 
Keep your code clean and simple so it is easy to read later and to easily add

echo '$problem_variable --->' .$problem_variable .<---'; die;

 
 
By the way, I am from the old school (with umpteen T-shirts) that shy away from variables that are global and prefer using locals instead. Far easier to trace problems.
 
 

 Signature 

Joke of the day     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 16 October 2008 04:12 AM   [ Ignore ]   [ # 8 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

With you new code you are not adding the partials to the master view file.

$glob_partials['title'] = 'My Great Title';

$master['content'] = $this->load->view('content_partial_view', $glob_partials, TRUE);
$master['sidebar']  = $this->load->view('sidebar_partial_view', $glob_partials, TRUE);

$this->load->view('master', $master);

This code is more readable and flexible. Lets say there are some other variables needed for the content then you can do

$glob_partials['title'] = 'My Great Title';

$content = array_merge($glob_partials,array('body'=>'long text'));

$master['content'] = $this->load->view('content_partial_view', $content, TRUE);
$master['sidebar']  = $this->load->view('sidebar_partial_view', $glob_partials, TRUE);

$this->load->view('master', $master);

Do you see now how something simple like naming arrays differently can make your code better.

Profile
 
 
Posted: 16 October 2008 04:43 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  633
Joined  12-26-2006

 
Hi Xwero,
 
I like your first example it is easy to read and understand at a glance.
 
Your second example is a wee bit too complex for me and not easy to read and understand at a glance.
 
I will endeavour to utilise your first example.
 
Many thanks for the tip.
 
 

 Signature 

Joke of the day     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 16 October 2008 04:47 AM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

What is too complex to understand in the second example?

Profile
 
 
Posted: 16 October 2008 05:27 AM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  633
Joined  12-26-2006
xwero - 16 October 2008 04:47 AM

What is too complex to understand in the second example?

 
The reason it is too difficult is there are too many variables on the one line.
 
To achieve the same result I would add an extra line and make the code (for me) easier to read at a glance.

//==========================================================================  
  
function _myView($msg=array('one', 'two', 'three'), $msg_name='$msg_name not passed') {
    $style
= 'text-align:left; font-size:1.0em; background:#ccc none; color:#00f;
              width:42%; margin:1em auto; border:dotted; font-weight:bold'
;
              
    echo
"<pre style='$style'>";
      echo
"Function: <b>" .__FUNCTION__ .' : ' .__FILE__ .' line(' .__LINE__ .')</b>' .BR;
      echo
'<br />';
      echo
'<b>' .$msg_name .'</b><br />';
      
print_r($msg);
      echo
'<br />';
    echo
"</pre>";
  
}
  
  
//==================================================
    
function index($start_page=5) {
    $glob_partials[
'title'] = 'My Great Title';

    
$content_Xwero          = array_merge($glob_partials, array('body'=>'long text'));
    
    
$content_John           = $glob_partials;
    
$content_John['body']   = 'long text';
  
    
// display the results   
    
$this->_myView($glob_partials, '$glob_partials');
    
$this->_myView($content_Xwero, '$content_Xwero');
    
$this->_myView($content_John, '$content_John');
    die;
  
}//end test

 
 
Here are the results

Function: _myView : C:\awww\ci_jokes\controllers\joke.php line(35)

$glob_partials
Array
(
    
[title] => My Great Title
)

Function:
_myView : C:\awww\ci_jokes\controllers\joke.php line(35)

$content_Xwero
Array
(
    
[title] => My Great Title
    [body]
=> long text
)

Function:
_myView : C:\awww\ci_jokes\controllers\joke.php line(35)

$content_John
Array
(
    
[title] => My Great Title
    [body]
=> long text
)

 
 
 

 Signature 

Joke of the day     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120536 Total Logged-in Users: 40
Total Topics: 126598 Total Anonymous Users: 3
Total Replies: 665552 Total Guests: 399
Total Posts: 792150    
Members ( View Memberlist )
Newest Members:  sweeper240andrew nsuntroprachel123Rach123Glenn StavaImfaelSMS!persanulandrewknight