Welcome Guest, Not a member yet? Register   Sign In
Specific Browser Views
#1

[eluser]HerQ[/eluser]
Hi there.

One of my clients wanted to serve different views according to the browser/platform. After having thought how to do so, I decided to write a CI_Loader subclass which handles this, with no extra work to do for the coder.

The idea is very simple. In your view folder, you need to have 2 subfolders. One called shared and one called specific. In the specific folder, you will have multiple folders for every platform you wish to create different views for. When a user is using a browser which is not recognized, you may put them in specific/others, but you can also save the default in the shared folder. When a specific view file exists for the used browser/platform, this is being used in favor of the shared one. Subfolders within all of this folder can be used as you would by default, and you have to load your view files just as you would normally, without specifying the shared or specific folder, this will be done for you automatically.

[DEPRECATED]
Browser detection is very limited at the moment, but for me it's enough to work properly. Defining browsers may be tricky when you don't know the idea behind it, but it's actually fairly easy.

To add support for a browser, simply add something like the following in _define_view_folders():
Code:
$browser['safari'] = array('Safari');
The key denotes the name of the folder used for that browser. All values in the array MAY be in the user agent. When all of the values MUST be in the user agent, use an array as a value in the main array. See comment above the (very limited) browser declaration for more information.

Of course you may want to change the browser detection system, I didn't want to include a whole class just for detecting Mobile Safari and MS Pocket IE, those are the major ones I have to support. There are some very neat classes available to determine the browser, or you can just use PHP's get_browser() function, but you must have the php_browscap.ini file in order for this to work.
[/DEPRECATED]

EDIT: Updated to 1.1. Now uses the CI User Agent library. There is now a config file where you can specify your browser routes. Because 'Mozilla' is in quite a few user agent strings, you may want to move 'Mozilla' to the end of the $browsers array in /system/application/config/user_agents.php.

When you have any questions or just have a good idea on how to enhance this subclass, let me know! I will consider updating the class when you have a good feature for it, so don't hesitate to let me know about it.
#2

[eluser]Phil Sturgeon[/eluser]
Have you looked at the User Agent library? It will do most of this work for you.
#3

[eluser]HerQ[/eluser]
Just updated. I must have overlooked that library, I found it when I had just opened this thread.




Theme © iAndrew 2016 - Forum software by © MyBB