CodeIgniter Forums
user guide, faq and layouts - I'm lost :( - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: user guide, faq and layouts - I'm lost :( (/thread-10592.html)

Pages: 1 2

user guide, faq and layouts - I'm lost :( - El Forum - 08-05-2008

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

user guide, faq and layouts - I'm lost :( - El Forum - 08-05-2008

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

2. Nest them

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

3. Return a string (that magical 3rd parameter - documented here)
$string = $this->load->view('content', $data, TRUE);

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?

user guide, faq and layouts - I'm lost :( - El Forum - 08-05-2008

Its all there, you just have to look deeper Smile - Click the table of contents in the upper corner

Quote: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);

user guide, faq and layouts - I'm lost :( - El Forum - 08-06-2008

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



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

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).

user guide, faq and layouts - I'm lost :( - El Forum - 08-06-2008

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.

user guide, faq and layouts - I'm lost :( - El Forum - 08-06-2008

Hi Peter222,

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

class Home extends Controller {

    function Home()
//... 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', $data, TRUE);
        $data ['leftcol']         = $this->load->view('common/leftcol_view', $data, TRUE);
        $data ['menu']             = $this->load->view('common/menu_view', $data, TRUE);
    // 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:
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

user guide, faq and layouts - I'm lost :( - El Forum - 08-06-2008

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:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class site_controller extends Controller {

function site_controller() {

function index() {

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

$data['header'] = $this->load->view('header_view', $data, TRUE);
$data['footer'] = $this->load->view('footer_view', $data, TRUE);

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

and views:

&lt;style type="text/css"&gt;
div {
border: 1px solid black;
margin: 10px;
padding: 10px;
<div id="header">

<div id="footer">
<a href="">Realized by...</a>, contact: &lt;?=$footer_email?&gt;


<div id="content">

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

user guide, faq and layouts - I'm lost :( - El Forum - 08-06-2008

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.

user guide, faq and layouts - I'm lost :( - El Forum - 12-07-2009

[eluser]Phil Sturgeon[/eluser]
[quote author="peter222" date="1218042772"]Thank You both! still no full example, but i catch it!! Smile[/quote]

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.

user guide, faq and layouts - I'm lost :( - El Forum - 12-22-2010

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.

Quote: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?

Quote: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?..