Mobile under TG 1.5

17 views
Skip to first unread message

Sam

unread,
Apr 20, 2012, 3:00:15 AM4/20/12
to TurboGears
I've spent a lot of time thinking about the best way to add a mobile
site to an existing TG 1.5 project.

I'd like to use the same urls for either the full or the mobile
version. Quora does this, and it seems to work well. It's less
confusing when sending links to people, making bookmarks, etc.

Just like Quora at the bottom of each page in the footer will be a
link to the mobile site if on the full site, or a link to the full
site if on the mobile site.

If one clicks one of these links, a cookie is saved for the session
that indicates the users preferred site version.

Now the controllers will use the same code base for each page but
different templates.

So I'd like to be able to indicate the templates like this:

@expose(template="genshi:foo.templates.bar",
mobile="genshi:foo.moblie_templates.bar")

Now what will happen here is that TG will automatically detect whether
the browser is mobile or not, and render the proper template. If a
cookie has previously been saved to override this, that template will
be rendered instead.

The footer that sets the cookies seems pretty straight forward.

Any advice on overriding expose to allow for the mobile template to be
explicitly named. And then doing the detection of whether the browser
is mobile or not?

Is there some other way I should be doing this?

Christoph Zwerschke

unread,
Apr 20, 2012, 10:44:07 AM4/20/12
to turbo...@googlegroups.com
Am 20.04.2012 09:00, schrieb Sam:
> Any advice on overriding expose to allow for the mobile template to be
> explicitly named. And then doing the detection of whether the browser
> is mobile or not?

The standard expose() decorator has an accept_format parameter which
allows choosing a different template based on the accept request header.

You could write a decorator that takes another parameter for choosing a
template based on other criteria like cookie values, but it's difficult.

Maybe the simplest solution would be to make the template parameter
callable. In that case the parameter would be called and the return
value taken as the template name. It's a simple enough feature to add it
to TG 1.5. We're using this idea in several other places already, and
it would allow for a lot of flexibility. In the callable you can make
all kinds of case distinctions using the cookies or any headers from the
cherrypy.request. I just tried that and it works nicely.

Anybody against such a change? Maybe this would be useful for TG2, too?

> Is there some other way I should be doing this?

You can alter the template dynamically in the controller like this:

http://turbogears.org/1.5/docs/ExposeDecorator.html#dynamically-switching-templates-within-a-function

-- Christoph

Christoph Zwerschke

unread,
May 2, 2012, 11:44:46 AM5/2/12
to turbo...@googlegroups.com
Am 20.04.2012 16:44, schrieb Christoph Zwerschke:
> Maybe the simplest solution would be to make the template parameter
> callable.

Implemented in r7420 now, will be available with TG 1.5.2.

-- Christoph

Daniel Fetchinson

unread,
May 5, 2012, 4:30:14 PM5/5/12
to turbo...@googlegroups.com
Great, thanks a lot Christoph!

Cheers,
Daniel


> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to
> turbogears+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.
>
>


--
Psss, psss, put it down! - http://www.cafepress.com/putitdown
Reply all
Reply to author
Forward
0 new messages