Welcome Guest, Not a member yet? Register   Sign In
How to handle views more simply ?
#1

[eluser]sikko[/eluser]
Hi everyone,

I'm looking for a way to handle views more simply.
Instead of including for each view, a header and a footer (in the view file):
Code:
<?php load_view('header_view') ?>
CONTENT STUFF
<?php load_view('footer_view') ?>
I would like to have only the content in the view file so that it'd look like this (without any pollution):
Code:
CONTENT STUFF

I've found this blog post dealing with this issue:
http://www.devshed.com/c/a/PHP/Handling-...Igniter/3/

The only problem now, is that I have to load these views, not in the view file, but in the controller class.
Code:
function index(){
// load 'header' view
$this->load->view('header_view',array('header'=>'Header Section'));
// load 'content' view and pass in database content
$this->load->view('content_view',array('users'=>$this->db->get('users')));
// load 'footer' view
$this->load->view('footer_view',array('footer'=>'Footer Section'));
}

Don't you know how could I make it less "intrusive" ?

Maybe could we put the header and footer stuff in a parent view class that would be loaded in every view ?

Thank you for your help.
#2

[eluser]vitoco[/eluser]
A simplle way it's to set a CONSTANT and use HOOKS, not the way i do it...but it's an idea

1.- in the function set a variable to "tell" the hook that you wanna add header an footer
Code:
function index()
{
    // SET HEADER AN FOOTER
    define( 'HEADER_FOOTER' , true );
    // load 'content' view and pass in database content
    $this->load->view('content_view',array('users'=>$this->db->get('users')));
}

2.- use "display_override" hook USER GUIDE : HOOKS


Code:
$CI =& get_instance();
// HEADER + CONTENT + FOOTER
if( defined( 'HEADER_FOOTER' ) AND HEADER_FOOTER )
{
    // echo 'header' view
    echo $CI->load->view('header_view',array('header'=>'Header Section') , true );
    // echo the 'content'
    echo $CI->get_output();
    // echo  'footer' view
    echo $CI->load->view('footer_view',array('footer'=>'Footer Section') , true );
}
// ONLY CONTENT
else
{
    // echo the 'content'
    echo $CI->get_output();
}
#3

[eluser]sikko[/eluser]
Thanks for your Good idea, I'll try. I didn't know hooks and it's a good feature.
And how do YOU do it ?
#4

[eluser]vitoco[/eluser]
calling views from views..old fashion way, but i'm used to
#5

[eluser]sikko[/eluser]
ok thank you for your help Smile
#6

[eluser]sikko[/eluser]
Hey I got a question,

Code:
// echo 'header' view
    echo $CI->load->view('header_view',array('header'=>'Header Section') , true );
Why the "true" in the load->view function ?
#7

[eluser]davidbehler[/eluser]
From the User Guide:
Quote:There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser.

That being said, these are interchangable:
Code:
echo $CI->load->view('header_view',array('header'=>'Header Section') , true );
and
Code:
$CI->load->view('header_view',array('header'=>'Header Section'));

You can use this to assign the data to a variable like this:
Code:
$string = $CI->load->view('header_view',array('header'=>'Header Section') , true );
and use this variable to do whatever you want with the content before actually showing it.
#8

[eluser]sikko[/eluser]
Thank you very much




Theme © iAndrew 2016 - Forum software by © MyBB