CodeIgniter Forums
Loading different views when on a mobile browser - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Loading different views when on a mobile browser (/showthread.php?tid=55492)



Loading different views when on a mobile browser - El Forum - 10-29-2012

[eluser]Prullenbak[/eluser]
Hi all,

I'm kinda new to codeigniter, and totally new to the forums, so bear with me Smile

I'm building a site which need to show different views to a user when he is on a mobile browser. Now, I've googled and searched here on the forum, but I can't seem to find an answer.

What I could do, is add a check for the user agent in every function, and load the appropriate views. However, this will clutter the code in my controllers, because I would need to do this whole if else stuff every time.

There was another solution I've found, which dealt with making some changes in the index, and copying that to a subfolder. I saw this meant the mobile version of the pages would have another url though. But I need the desktop and mobile version of every version to have exactly the same URL.

So, what I would like very, very much, is to be able to let CI do this automatically for every view that's loaded, in the entire application:
If the user is on a mobile browser, always look for view files in the views/mobile/ folder, and if he's not on mobile, always look in the views/default/ folder.

Is this possible?

Thanks a lot in advance,

Prullenbak

By the way: Some other similar questions to this one, where getting answers along the lines of: "Build it responsively with css, mediaqueries, javascript, etc...But as much as I would like to do that (and have done that before), The design (and the differences between the mobile and default versions) doesn't lend itself for responsive webdevelopment, at all Sad



Loading different views when on a mobile browser - El Forum - 10-30-2012

[eluser]Alex Florea[/eluser]
Yes. It is posible. Use User Agent Class from CI.

http://ellislab.com/codeigniter/user-guide/libraries/user_agent.html


Loading different views when on a mobile browser - El Forum - 10-31-2012

[eluser]Prullenbak[/eluser]
I know that, that's what I mentioned as my first solution. But the question is: is there a way to put the 'check user agent'-code on one place in the application, and make it load views from another view-folder by default from that point on.

Thanks Smile


Loading different views when on a mobile browser - El Forum - 11-01-2012

[eluser]rogierb[/eluser]
Have you tried MY_controller?

Once you determine its a mobile device, you can set a var that determines which folder to load it from.
I use a helper that load a specific folder/template depending on device.

Code:
//helper name: view($folder_to_load, $data_to_pass)
view('mobile, $data);



Loading different views when on a mobile browser - El Forum - 11-05-2012

[eluser]Prullenbak[/eluser]
I'm not sure I understand correctly...

Do you mean I write my own helper, that checks the user_agent and loads the appropriate view with $this->load-view(), and then use that helper everytime I want to load a view, instead of $this->load->view() ?

If so, sounds like a plan Smile


Loading different views when on a mobile browser - El Forum - 11-05-2012

[eluser]rogierb[/eluser]
Well almost.

Checks like the user agent should be done at the earliest, hence in the MY_Controller.
If its a mobile device one can set a flag and use that to load a smaller set of data for instance.

The helper can use this flag to determine which view.

Its not just the view, remote devices often require different css, images and datasets. All depending on screen size and screen resolution




Loading different views when on a mobile browser - El Forum - 11-11-2012

[eluser]Unknown[/eluser]
555