Welcome Guest, Not a member yet? Register   Sign In
Best place to put a method that prepare the views ?
#1

[eluser]Rushino[/eluser]
Hello,

I have a method in my controller called renderViews. It actually take 3 views to render to the user.

I am afraid that this method get repeated in each controller.. the problem is that this method use the load function. So it may be needed in the controller.. but i try to find a way to relocalize this method.

So what is the best place to put this method ?

Here a sample of this method..

Code:
private function _renderView($titrePage, $curPage, $contentData, $headerHTML = 'headers/default', $footerHTML = 'footers/default')
    {
        // Create data object for the header        
                $headerData['titre'] = $titrePage;
        $headerData['navigation'] = $this->_generateNavigation($curPage);
        
        // Create data object for the footer
        $footerData['copyright'] = "APP_COPYRIGHT";
        
        // Render to views
        $this->load->view('headers/default', $headerData);
        $this->load->view($curPage, $contentData);
        $this->load->view('footers/default', $footerData);
    }

Thanks.
#2

[eluser]InsiteFX[/eluser]
MY_Controller

InsiteFX
#3

[eluser]Rushino[/eluser]
Yeah i know but i was wondering if this could be placed in a lib instead?
#4

[eluser]InsiteFX[/eluser]
You could put it in a library, but then you would need to use the CI Super Object to access your views.
Something like this:
Code:
private function _renderView($titrePage, $curPage, $contentData, $headerHTML = 'headers/default', $footerHTML = 'footers/default')
    {
        $CI =& get_instance();

        // Create data object for the header        
        $headerData['titre'] = $titrePage;
        $headerData['navigation'] = $this->_generateNavigation($curPage);
        
        // Create data object for the footer
        $footerData['copyright'] = "APP_COPYRIGHT";
        
        // Render to views
        $this->CI->load->view('headers/default', $headerData);
        $this->CI->load->view($curPage, $contentData);
        $this->CI->load->view('footers/default', $footerData);
    }

InsiteFX
#5

[eluser]Rushino[/eluser]
I believe this is bad practice ? if so then i will keep it in the controller.

Thanks.
#6

[eluser]InsiteFX[/eluser]
And how do you figure it is bad practice?

I use something similar to load my views in a library for displaying forms in the library!

InsiteFX
#7

[eluser]Rushino[/eluser]
Because its a library. You shouldn't call a super object from a framework directly in a lib. You make the lib depend on the framework. Well, that how i see it.. no offence here. I pretty much doesnt know myself what is really good or bad code.. hard to figure out. But from my first impression i though it would be bad to call some super object directly in the lib. But that would definitivly fix "the same code in all the controllers" problem.
#8

[eluser]InsiteFX[/eluser]
It's is common partice to call the CI Super Object in a library, It's the only way to call any other CI methods from models database etc!

You need to read up on managing your applications!

InsiteFX
#9

[eluser]Rushino[/eluser]
Alright thanks.




Theme © iAndrew 2016 - Forum software by © MyBB