Welcome Guest, Not a member yet? Register   Sign In
Header/Footer layout best practice?
#1

[eluser]spyro[/eluser]
When I started building my CI application I extended the Controller with MainPage.php so that every time I extended MainPage with a new controller I could automatically get the header and footer in the page without having to call the header/footer each time. MainPage looks as follows.

Code:
class MainPage extends Controller {

    function MainPage()
    {
        parent::Controller();
        $this->config->load('config');
    }
    
        // Handle loading header/footer and any other content that all main pages will have
    function index()
    {

        if ($this->session->userdata('logged_in') == TRUE)
        {
            $this->load->view('accountHeader_view');
        }
        else
        {
            $this->load->view('mainHeader_view');
        }
                
        $this->page_content();
        
        $this->load->view('mainFooter_view');
    }
        
    function page_content()
    {
            // Over-ride me in sub-class!
    }
}

The problem that I have now is that I have no way to set the title or metadata in the header view because it is being extended.

Is there a way to correct this with my current layout?

If not, what is the best approach for headers/footers that also minimizes code?
#2

[eluser]Jelmer[/eluser]
I do it through a wrapper view, a lot easier and works like a charm. You call the view like this:
Code:
$data['view'] = 'example'; // the name of the view you want to load
$data['view_data'] = array('example' => 'test'); // the data for the specific view you want to load
$data['title'] = 'Example title'; // title for the page
$this->load->view('wrapper', $data);

And then your wrapper.php view looks like this:
Code:
<html>
<head>
    <title><?php echo $title; ?></title>
</head>

<body>

<?php
    $this->load->view($view, $view_data);
?>

</body>

</html>

with the example.php view:
Code:
<p>&lt;?php echo $example; ?&gt;</p>

Which gives as output:
Code:
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Example title&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

<p>test</p>

&lt;/body&gt;

&lt;/html&gt;

You could also use the forum's search function as this question has been asked countless times before, and there are other good solutions...
#3

[eluser]The Wizard[/eluser]
i do it with a custom system ive developed for it,
i store the files in

views\themes\default
-----------------------header
-----------------------footer
-----------------------pages
-----------------------------foldername1
-----------------------------foldername2
-----------------------------master-file_name.php

i setup the themeing system from within the controller,
load the them_folder_variable + master-file_name which will look
in the themes\ folder with the actual selected theme (which in our,
example is default). IF the master file loaded, will need another
file releated to that function, it looks into its own DIRECTORY
in this example "foldername1".

the code looks like:

model_theme
Code:
&lt;?php

class Model_theme extends Model
{

    function _construct()
    {
        parent::Model();
    }

    function Theme_returnThemeInfo( &$data_theme, $name_directory )
    {

        $this->load->helper('url');

        $data_theme ['theme_name']          = 'default';
        $data_theme ['theme_name_page']     = $name_directory;

        $data_theme ['theme_folder']        = 'themes/'. $data_theme['theme_name'] .'/';
        $data_theme ['theme_folder_pages']  = 'themes/'. $data_theme['theme_name'] .'/pages/';
        $data_theme ['theme_folder_vault']  = 'themes/'. $data_theme['theme_name'] .'/pages/'. $name_directory .'/';


        $data_theme ['theme_path']          = base_url() . 'system/application/views/' . $data_theme['theme_folder'];
        $data_theme ['theme_path_pages']    = base_url() . 'system/application/views/' . $data_theme['theme_folder_pages'];
        $data_theme ['theme_path_vault']    = base_url() . 'system/application/views/' . $data_theme['theme_folder_vault'];

        //return $data_theme;
    }


}

example controller load code
Code:
/* Theme System with Reference Variable. */
            $this->model_theme->Theme_returnThemeInfo( $data, 'meeting' ); //<-- meeting being the folder name

            $this->load->view( $data['theme_folder_pages'] . 'master-set_album', $data ); //master file


example master file:
Code:
&lt;? $this->load->view( $theme_folder . 'header/normal/header.php'); pretty self explaining ?&gt;

    &lt;? $this->load->view( $theme_folder_vault . 'list.php' ); //folder_vault referrs to the special FOLDER ?&gt;

&lt;? $this->load->view( $theme_folder . 'footer/normal/footer.php') ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB