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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

[SOLVED] User Agent Detection


Go to End


9 Posts   3580 Views

Avatar
zenmonkey

Community Member, 545 Posts

27 August 2009 at 12:15pm

Edited: 27/08/2009 11:44pm

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
dhensby

Community Member, 253 Posts

27 August 2009 at 12:50pm

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

Community Member, 545 Posts

27 August 2009 at 4:34pm

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

Community Member, 545 Posts

27 August 2009 at 11:43pm

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
dhensby

Community Member, 253 Posts

28 August 2009 at 12:37am

Have you thought about rendering with different templates?

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

Avatar
zenmonkey

Community Member, 545 Posts

28 August 2009 at 1:00am

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

Avatar
steve_nyhof

Community Member, 224 Posts

19 January 2010 at 5:26pm

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

Avatar
AdamJ

Community Member, 145 Posts

19 January 2010 at 6:52pm

Steve, I don't know about their solution, but maybe check out this post on ssbits.com

Go to Top