Welcome Guest, Not a member yet? Register   Sign In
Parse template once
#1

[eluser]xinq88[/eluser]
Hi all,

here I am, with another problem for you guys Tongue. Hope you can help me.
I've got the following function of every controller (implemented in the controller class):

Code:
private function ParseTemplate()
    {    
        // Set the view
        $this->partials = $this->view;
    
        // Load all data in to the template 'maintemplate'
        $this->template->load('maintemplate', $this->partials, $this->data);
    }

In other controllers I set the data and the views. So I've got multiple functions, all the functions call the ParseTemplate method at the end of the function. When I use only one function for a view, this works great, but when I've the following code:

Code:
public function FunctionOne($cat_id = 0)
    {    
        $this->data['query_boekenlijst'] = $this->boek->GetLijst($this->cat_id);
        $this->view['content'] = 'boeken/boekenlijst';
        $this->ParseTemplate();
    }
    
    public function FunctionTwo($id)
    {
                $this->view['sidebar'] = 'sidebar';
                if ($someCondition == true) $this->FunctionOne();
        $this->ParseTemplate();
    }

In this case, the template will be parsed twice, because the two functions but call the ParseTemplate method.

I can't just delete the ParseTemplate function in 'FunctionTwo' because $someCondition is not always TRUE.

So my question:
How can I fix this problem, is there somehow a way of calling a method always at the end of the controller, or something like that?

I hope it's clear to you guys.
Thank you all for your time!
#2

[eluser]Dready[/eluser]
Hello,

perhaps add a flag to tell FunctionOne if it have to call parseTemplate ? A thing like :

Code:
public function FunctionOne($cat_id = 0)
    {    
        $this->data['query_boekenlijst'] = $this->boek->GetLijst($this->cat_id);
        $this->view['content'] = 'boeken/boekenlijst';
        if ( isset($this->call_parser) && $this->call_parser != 'no' ) {
               $this->ParseTemplate();
        }
    }
    
    public function FunctionTwo($id)
    {
                $this->view['sidebar'] = 'sidebar';
                if ($someCondition == true) {
                   $this->call_parser='no';
                   $this->FunctionOne();
                }
        $this->ParseTemplate();
    }
#3

[eluser]xinq88[/eluser]
Yeah, that's definitely a solution. I just hope there is some function which is called every time a controller is done. So basically a sort of 'ControllerEnd' function.
#4

[eluser]Dready[/eluser]
I follow your opinion: CI is missing a "ControllerStart" and "ControllerEnd" function, beside PHP's constructors and destructors.
#5

[eluser]xinq88[/eluser]
So basically what you're saying is that I've to use flags for these problems?
#6

[eluser]Dready[/eluser]
Ci does not provide the type of function you are needing. Perhaps you can test with PHP destructors if you're using PHP5, or use flags.
#7

[eluser]xwero[/eluser]
You can use the post_controller hook to do things like this. A way to use it is to autoload a rendering library that receives all the data and templates that need to be displayed and in the post_controller hook you put the rendering method.




Theme © iAndrew 2016 - Forum software by © MyBB