Re: Template Loaders and Acquisition Mechanism

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, October 8, 2009, 3:25:15 PM, Darko Radosevic wrote:

> Hi,
>
> we have templates placed in folder hierarchy (default/client/site)
> and we're using default FileTemplateLoader and acquisition mechanism
> to load templates. But now we want to introduce a brand new type of
> templates - templates for mobile site. The mobile templates should
> follow this same default/client/site hierarchy but they should take
> precedence over regular templates when the request is coming from a
> mobile device. We could diferrentiate mobile templates by placing in
> separate folder or by "m_" name prefix, we haven't decided yet. So,
> loading order on a mobile site should be:
>
> 1. mobile site template 2.mobile client 3 mobile default 4.standard
> site 5 standard client 6.standard default
>
> This looks tricky to solve, I've first tried to utilize
> MultiTemplateLoader and some custom TemplateLoaders, but I would get only this loading order:
>
> 1. mobile site 2.standard site 3.mobile client 4. standard client
> 5. mobile default 6.standard default.
>
> The only working solution I've found so far is to use two
> Configurations,

Don't do that, it's not how Configurations meant to be used. Templates
returned by one of the Configuration-s will search their dependences
using that Configuration. This also means that you will end up some
templates cached twice... and who knows what else I didn't realized
right now.

> each having different base folder. In case 'mobile'
> Configuration throws FileNotFoundException, try to use 'standard'
> Configuration. The problem with this solution is that we'll need
> copy all templates from the 'standard' folder to the 'mobile' folder and then maintain duplicates.
>
> The only other possible solution I see is to rewrite TemplateCache
> to maybe somehow  move acquisition mechanism inside template loader,
> but this looks way too low-level...

You couldn't even do that there in a way so it will work correctly.

> Do you know of any cases where TemplateCache is overwritten with
> some diferrent implementation, or do you know of some Freemarker
> features that could be useful for me?

Maybe you can do the trick with the localization feature... like if
being mobile optimized would be a locale variation (you know, the
optional 3rd part of the locale string). Localization quite much does
what you need, in that it first tries to find the most locale-specific
template variation with acquisition, and only when that fails tries it
to find a less-specific one.

Now if it doesn't work out well with localization, you can still
submit a patch. But then let's see what should be achieved... you want
to specify the target *media* here. It's very much like localization
right now, so it should look similarly: [email protected],
mytemplate_@mobile_en.ftl, mytemplate_@mobile_en_US.ftl, etc. It's @
because that's also what CSS uses for media selection, and it's also
suggestive: the page "at a mobile", "at a printer". I think the media
should have higher priority than locale, and that's also what you
wanted here. If a different need arises, we can still add a
media_priority setting. Yeah, and target_media itself is a new
setting. Pretty much like locale.

Objections?

Of course, you may ask for some guidance if you want to do the patch.

> Thanks,
> Darko

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.