Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

[SOLVED] User Agent Detection


Go to End
Reply

9 Posts   2327 Views

Avatar
zenmonkey

27 August 2009 at 12:15pm (Last edited: 27 August 2009 11:44pm), Community Member, 528 Posts

I'm trying to write a small script to Detect User-Agent but I'm having a total n00b problem. Where do I put it, does it extend the Page_Controller or another one?

Avatar
Pigeon

27 August 2009 at 12:50pm Community Member, 243 Posts

I would try it in the Page class.

function Browser() {
...
return [browser];
}

and then address it in the template as:

<% if Browser = IE %>
...
<p>You're using $Browser! Naughty!</p>
...
<% end_if %>

But i don't know how you want to use it... You can then address it in the Page class as $this->Browser()

Avatar
zenmonkey

27 August 2009 at 4:34pm Community Member, 528 Posts

I have it in the page.php right now in the Page_Controller class but I think it would be more useful if it could be run at config so that you could conditionally load different templates. I'm using it for phone detection. It returns either iPhone, SmartPhone WAPPhone or NoPhone. That way you wouldn't have to build multiple versions of the theme in one theme file.

Avatar
zenmonkey

27 August 2009 at 11:43pm Community Member, 528 Posts

I ended up remembering another forum post about the forcing conditional css files to render under the theme css and used the same technique.

If I have time later I'll post my full code.

Avatar
Pigeon

28 August 2009 at 12:37am Community Member, 243 Posts

Have you thought about rendering with different templates?

http://doc.silverstripe.org/doku.php?id=templates#calling_templates_from_php_code

Avatar
zenmonkey

28 August 2009 at 1:00am Community Member, 528 Posts

I'll test it out, it may be an more elegant solution Thanks

Avatar
steve_nyhof

19 January 2010 at 5:26pm Community Member, 224 Posts

Can you please post you code to how you did this and where?

Avatar
AdamJ

19 January 2010 at 6:52pm Community Member, 145 Posts

Steve, I don't know about their solution, but maybe check out [url=http://ssbits.com/detecting-mobile-browsers-to-re-theme-your-site/]this post on ssbits.com[/url]

Go to Top