Welcome Guest, Not a member yet? Register   Sign In
user guide, faq and layouts - I'm lost :(
#1

[eluser]peter222[/eluser]
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?
#2

[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:
Code:
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer')

2. Nest them
Code:
$this->load->view('template')

// 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)
Code:
$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?
#3

[eluser]SpooF[/eluser]
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-gui...views.html

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:
Code:
$data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );

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

And here's an example using an object:
Code:
$data = new Someclass();
$this->load->view('blogview', $data);
#4

[eluser]peter222[/eluser]
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).
#5

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

[eluser]andreagam[/eluser]
Hi Peter222,

the key is your controller.
Let's have a minified example:
Code:
&lt;?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', $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);    
    }
}
?&gt;

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:
Code:
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
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
#7

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

Code:
$route['default_controller'] = "site_controller";

Then, controller:
controllers/site_controller.php
Code:
&lt;?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']="[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);
   }
}
?&gt;

and views:

views/header_view.php
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$site_title?&gt;&lt;/title&gt;
&lt;style type="text/css"&gt;
div {
border: 1px solid black;
margin: 10px;
padding: 10px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="header">
&lt;?=$header_txt?&gt;
</div>

views/footer_view.php
Code:
<div id="footer">
<a href="http://www.mysite.com">Realized by...</a>, contact: &lt;?=$footer_email?&gt;
</div>

&lt;/body&gt;
&lt;/html&gt;

views/layout_view.php
Code:
&lt;?=$header?&gt;
<div id="content">
&lt;?=$content?&gt;
</div>
&lt;?=$footer?&gt;

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

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

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

[eluser]Unknown[/eluser]
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?..


Thanks




Theme © iAndrew 2016 - Forum software by © MyBB