Welcome Guest, Not a member yet? Register   Sign In
redirecting view based on useragent
#1

[eluser]sooner[/eluser]
I wrote a MY_Contoller which extends the controller..in the constructor i check the user agent and based on that redirect to different templates...

lets say one of my template look like

header

content

footer

when i create a new controller and load a view

$this->load->view('example');

example is a view for that controller.


so when this example view loads i want to have the example view content inside the template i.e example view content should replace the content in template.

how do i do that..i see many approaches for view embedding but all of them ask me to load the main template view in the controller i am calling from...since in my approach i am loading the template view in the MY_Controller it does not have the content(here it is example view) at that time.....but my template view differs based on the user agent..any suggestions plz
#2

[eluser]Réjôme[/eluser]
What about that (not tested !) :

Code:
<?php

class MY_Controller extends Controller  {

    // private prepared header & footer
    private $header;
    private $footer;
    
    function MY_Controller()
    {
        parent::Controller();
        
        // Test of the useragent
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/^Mozilla\/.*?Gecko/i',$agent))
        {
            // process here for firefox browser
            $header = $this->load->view('ff-header', '', true);
            $footer = $this->load->view('ff-footer', '', true);
        }
        else
        {
            $header = $this->load->view('ie-header', '', true);
            $footer = $this->load->view('ie-footer', '', true);
        }
        
    }

    // Display function
    protected function render($view, $data = null)
    {
        // header
        echo $header;

        // page
        $this->load->view($view, $data);

        // footer
        echo $footer;
    }

}

In the application controllers, you never call
$this->load->view()
but always
$this->render()


Let me know if this helped :-)
#3

[eluser]sooner[/eluser]
thx for the reply...but the problem here is you need to include header and footer for every controller...i need a way to just include the header and footer in the main template and from controller just load a view that put contents on the main template..but i found a different solution by setting the main template based on the user agent in the extended controller constructor and using the value in every controller to pass to the view..

for example:

the constructor sets $this->nameoffile based on useragent..here nameoffile is the main template you need to load based on user agent and it will contain the header and footer.

and then in the controller

$this->load->view($this->nameoffile);
#4

[eluser]markup2go[/eluser]
sooner, Réjôme's method allows you to extend the MY_Controller class, which you will probably end up doing anyways. Therefor the render() function will be available to whichever controllers you decide.

So just do what he said and you will have a damn header and footer sir.

http://codeigniter.com/wiki/MY_Controlle...ontroller/

You can also use a hook to accomplish something similar, but I prefer Réjôme's method myself.
http://ellislab.com/forums/viewthread/57902/
#5

[eluser]sooner[/eluser]
markup2go, thx for ur reply those extending the controller its all done...my problem was not on that..my issue was different..but i found a way to do it as i have described early. the topic may indicate my problem is redirecting...but my problem is exactly redirecting to different main template,which is done too.But loading different views which should show up in the different main template i redirected.at that place i found the problem...




Theme © iAndrew 2016 - Forum software by © MyBB