EllisLab text mark
Advanced Search
     
user guide, faq and layouts - I’m lost :(
Posted: 05 August 2008 08:21 PM
Avatar
Joined: 2008-08-05
35 posts

I looked for good PHP framework. Google “said”, that CI is very good documented so I decided to try it. Since three days I try to find basics info like layouts with dynamic partials.

1. No info in main user guide. Some words about multiple views but I still don’t know how to load dynamic data to header, footer, etc.
2. Wiki - no FAQ on home page of Wiki, why?. I found it only by google (http://codeigniter.com/wiki/FAQ/)...
3. ...this FAQ says now “Version 1.6 (Released January 31, 2008) has support for multiple views.” So I see that rest of section is “old school” (depraced?). Ok, but go back to point 1.
4. Some examples in forum says “$page = $this->load->view(“default/header.php”, null, true);” but no info in user guide what is ‘true’ parameter.
5. I found “layout library” but still can’t understand how to use it with more partials like header, footer, navigations…

I’m really annoyed, can’t start because I can’t see actual orderly FAQ to 1.6.3 and good documented basics like layouts with dynamical partials. I don’t want to waste time for reading of hundreds posts in forum to find best practices. Most of them are not actual I belive. So, I suggest to put official info in user guide or FAQ, how to use it in CI. I’m beginner PHP programmer, maybe advanced users only can use CI?

 
Posted: 05 August 2008 08:45 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
2540 posts

First things first, welcome to CodeIgniter,

I’m sure we can get you sorted out so you can start with the real work.  I think in this case you’re looking for something that isn’t there (I really appreciate that you did your homework though).  There is no ‘official’ way to build your output.  It’s totally up to you.  In a very basic fashion, CodeIgniter lets you do 3 things with views:

1. Load them sequentially:

$this->load->view('header');
$this->load->view('content');
$this->load->view('footer'

2. Nest them

$this->load->view('template')

// Inside the template view
<?php $this->load->view('header');?>
<div id="content">
<?php $this->load->view('content');?> 

3. Return a string (that magical 3rd parameter - documented here)

$string $this->load->view('content'$dataTRUE); 

The second parameter is data you want to pass to a view.  It should be an associative array, and the array keys become your new variable names.  It’s available to all nested templates, so you don’t have to pass it in again.

How you setup your app is up to you, it will probably be a mixture of the 3.  I personally like to make a template library.

Does that help?

 Signature 
 
Posted: 05 August 2008 08:50 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-25
195 posts

Its all there, you just have to look deeper smile

http://ellislab.com/codeigniter/user-guide/ - Click the table of contents in the upper corner

http://ellislab.com/codeigniter/user-guide/general/views.html

Adding Dynamic Data to the View

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:

$data = array(
               
'title' => 'My Title',
               
'heading' => 'My Heading',
               
'message' => 'My Message'
          
);

$this->load->view('blogview'$data); 

And here’s an example using an object:

$data = new Someclass();
$this->load->view('blogview'$data); 
 
Posted: 06 August 2008 03:33 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-05
35 posts

Thanks for answer but both posts are identical as many infos which I described before. Maybe I can’t understand something very clear for You but my brain is still blocked smile


inparo:

$this->load->view(‘header’);
$this->load->view(‘content’);
$this->load->view(‘footer’)

Where in Your example dynamically data is transferred to header, footer? It was my main question.

SpooF:
I really know this part of user_guide (multiple views). But like above. Where is data transferred to header and footer in this example?

Maybe my question was not clear, I would be very graceful if somebody write me a full example, how should look files:

header controller (with some dynamically data like selected position of menu)
header viewer (how to display dynamically transferred data)

content controller (with some dynamically data)
content viewer (how to display dynamically transferred data)

footer controller (with some dynamically data like contacts with different emails dependent of section)
footer view (how to display dynamically transferred data) 

and main, in result: layout - how to use it in any controller/viewer withs all components above (header,content, footer)

I’m sure that many, many people will be happy to see full example, not only fragments because many beginners dont’t understand how to join it all (sorry for my english).

 
Posted: 06 August 2008 04:22 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-25
195 posts

Have you checked out the video tutorials on the main page? Main reason I ask is because I think your off to the wrong start on how a controller is used. A controller is a way to organize parts of your site such as a blog, gallery, and such. Not so much particular parts of a page. Even though it can be using some custom libraries.

The tutorials on the main page will give you an amazing start into Code Igniter.

http://codeigniter.com/tutorials/

 
Posted: 06 August 2008 04:46 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2006-11-05
96 posts

Hi Peter222,

the key is your controller.
Let’s have a minified example:

<?php

class Home extends Controller {

    
function Home()
    
{
        parent
::Controller();    
        
$this->load->helper('file');
        
$this->load->helper('form');    
//... here you can load helpers, libraries, etc.
    
}
    
    
    
function index()
    
{                
                        
    
// CREATING DYNAMIC CONTENT ////////////////////////
        
$data ['title'"My Website"
        
$data ['footer']    'A typical footer';            
    
// end DYNAMIC CONTENT //////////////////////////////
                
    // LOADING PARTIALS //////////////////////////////////////////////////////////////////
        
$data ['header']         $this->load->view('common/header_view'$dataTRUE);
        
$data ['leftcol']         $this->load->view('common/leftcol_view'$dataTRUE);
        
$data ['menu']             $this->load->view('common/menu_view'$dataTRUE);
    
// END PARTIALS /////////////////////////////////////////////////////////////
        
        
$this->load->view('content/home_view'$data);    
    
}
}
?> 

The “_view” files are basically HTML files, where dynamic data comes from the controller.
For example, to retrieve the Title in your header_view.php or home_view.php you just echo like that:

<title><?=$title?></title

What you put in the $data array in your controller is available with the array key as a variable name.

I hope that helps.
Enjoy Codeigniter

 Signature 

When a good coder dies, he degrades gracefully…

My Website |
Follow me on Twitter

 
Posted: 06 August 2008 09:12 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-05
35 posts

Thank You both! still no full example, but i catch it!! smile

SpooF: I watched tutorials many times, but I think procedural PHP still so it was all new for me. Yes, I persisted that all view must have own controller - It was my main mistake.

CI andrea: it was still incomplete example but thanks to You and SpooF i finally catch it smile))

All works now, My full minified example, for other people whicth has problem like me to understand idea:

in config/routes.php i changed line:

$route['default_controller'"site_controller"

Then, controller:
controllers/site_controller.php

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class 
site_controller extends Controller {

function site_controller() {
      parent
::Controller();
   
}

function index() {

$data[
'footer_email']="test@test.com";
$data['header_txt']="heder text";
$data['content']="Main content here...";
$data['site_title']="My test site";

$data['header'$this->load->view('header_view'$dataTRUE);
$data['footer'$this->load->view('footer_view'$dataTRUE);

$this->load->view('layout_view'$data);
   
}
}
?> 

and views:

views/header_view.php

<html>
<
head>
<
title><?=$site_title?></title>
<
style type="text/css">
div {
border
1px solid black;
margin10px;
padding10px;
}
</style>
</
head>
<
body>
<
div id="header">
<?=$header_txt?>
</div

views/footer_view.php

<div id="footer">
<
a href="http://www.mysite.com">Realized by...</a>, contact<?=$footer_email?>
</div>

</
body>
</
html

views/layout_view.php

<?=$header?>
<div id="content">
<?=$content?>
</div>
<?=$footer?> 

It works for me, maybe something need any correct, let somebody correct me. Thanks again all for help!

 
Posted: 06 August 2008 11:00 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

nicely done, CI denizens.

this thread exemplifies the patient and helpful spirit of the devs that surround and support this framework.

I’ve put a link to this thread in the Views section of the FAQ.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

 
Posted: 07 December 2009 07:46 AM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-11
2987 posts
peter222 - 06 August 2008 01:12 PM

Thank You both! still no full example, but i catch it!! smile

Instead of doing this in EVERY controller, you can google search for MY_Controller to learn how to set default data for your entire app.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

 
Posted: 22 December 2010 02:21 AM   [ # 9 ]   [ Rating: 0 ]
Joined: 2010-12-22
1 posts

Hi all. Am new to CI and i have done exactly as mentioned above. I have many views such as header, footer, main_content and sidebar.

What i am interested is in getting dynamic content for all these.

For example, i have home controller, where i call all the views i.e(header, footer, main_content and sidebar). All these are displayed in home.php view. So far so good.

Now i want to get data for all these views (header, footer, main_content and sidebar) from database. How do i do it? Where should i write the code to query db for each view. Should it be in home controller or should it be directly in header view?

Also, i have another page say “about us” and the link is in header. How do i keep the layout of this link same as the home and get different data depending on the link. What i want to know whether i should create another controller, view and how the flow will be?..


Thanks

 
Posted: 22 December 2010 10:37 AM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

hi noobcode,

welcome to CI!

yes, in general it is OK to put your db access code in the controller when you are first trying out CI. but, as you progress, you will find that most people build a “model” and keep all of the db code in there and pass the results back to the controller.

the “problem” with CI is that you have to learn what architecture suits you (and your application) best. there are so many ways to set things up that it can get confusing to know which is best. my advice is to just try one way that seems straightforward and try it to see.

in any case, don’t put db access code in the view - it will ruin your future days.

as for your last question, it sounds like you might want to look at a template library. or maybe just use the returned value from views called in a controller to build your layout.

cheers.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

 
Posted: 23 December 2010 05:27 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2010-12-10
25 posts

Thanks to all, i also new here so i get the basic info. thanks…..  smile