Welcome Guest, Not a member yet? Register   Sign In
OOP and templates
#1

[eluser]Unknown[/eluser]
Hello, i have just started with OOP and CodeIgnitor.
Before OOP, when i created a template, i was using the include function, so i could keep some contents in every page, for example the header block and the footer.

I can see, in CodeIgnitor and OOP this is done different.
So far i have put these commands in every function i made in the controller:

$this->load->view('top');
$this->load->view('footer');

but i dont like to repeat this every time for every function, it doesnt seem right to me.

What is the correct set up for this task ?
#2

[eluser]bl00dshooter[/eluser]
[quote author="peturkirke" date="1293848556"]Hello, i have just started with OOP and CodeIgnitor.
Before OOP, when i created a template, i was using the include function, so i could keep some contents in every page, for example the header block and the footer.

I can see, in CodeIgnitor and OOP this is done different.
So far i have put these commands in every function i made in the controller:

$this->load->view('top');
$this->load->view('footer');

but i dont like to repeat this every time for every function, it doesnt seem right to me.

What is the correct set up for this task ?[/quote]

Have you tried loading the top view on the __construct function and the footer on the __destruct function? You could also extend the Loader library to load these views by default. Or maybe you could use an alternative solution to CI's.

Many ways to achieve want you want...
#3

[eluser]Unknown[/eluser]
I implemented the __construct functions solution, seems like it workes fine.

Thanks for your help.
#4

[eluser]InsiteFX[/eluser]
Read this Article.

CodeIgniter Base Classes: Keeping it DRY

InsiteFX
#5

[eluser]edjon2000[/eluser]
Hi peturkirke seasons greetings,

I came across the same problem,

My solution was as follows(I picked this up from one of the CI tutorials I came across),

In my views folder I created an additional folder called includes and in that I put my common header file and common footer file as well as a template file like so:-
Code:
<?php $this->load->view('includes/header'); ?>

<div id="main-content">

&lt;?php
if(isset($featured)) $this->load->view($featured);

if(isset($main_content_1)) $this->load->view($main_content_1);

if(isset($main_content_2)) $this->load->view($main_content_2);
?&gt;

</div>&lt;!--end of div main-content--&gt;

&lt;?php $this->load->view('includes/footer'); ?&gt;
So, for any views I would use something like this in my controller methods:-
Code:
function index()
    {
        $data = array();

        $data['page_title'] = 'Home';
        $data['extra_head_content'] = '[removed][removed]';
        $data['featured'] = 'site_views/featured_area_view';
        $data['main_content_1'] = 'site_views/home_view';
        $data['main_content_2'] = 'site_views/forms_view';

        $data['vac_name'] = $this->vacancy_model->generate_vacancy_list();
        $data['sec_name'] = $this->vacancy_model->generate_sector_list();

        $data['vacancies'] = $this->vacancy_model->get_vacancies(array('vacancy_featured' => 'Yes', 'vacancy_active' =>'Yes' ));

        $this->load->view('includes/template', $data);
    }
Effectively I am only changing the content between the header and the footer and of course I can make available any data for the view, this solution works for me, it may not be the best way, but it has certainly helped me, coming from a traditional PHP/MySQL background and trying my first steps in CI

Jon




Theme © iAndrew 2016 - Forum software by © MyBB