Changing findTemplateSource behavior

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Changing findTemplateSource behavior

Ruben Trancoso
Hello there,

I started using freemarker in a project where I would like to find templates from database as well from filesystem.

My expectation was to have a signature on TemplateLoader where I could get the template name and the localeName separetely so I could query the db.

This requirement came from the idea that on the front end the user will add the name and the locale on colluns like this:

some.template, en_US
some.template, pt_BR
some.template, pt_PT
some.template, en
...
some.other.template, jp_JP

but findTemplateSource blend the name and locale and perform it's fallback strategy, and also expect that all files are suffixed with .ftl (or something) resulting in lookup keys like this:

some_en_US.template, some.other_pt_BR.template.

My first Idea was to extend TemplateCache, but it should break compatibility with the TemplateLoader interface when it comes to be a File.

So I was wondering if some experienced user/developer could put some light on how to achieve this without much pain and preserving the code structure.  .

//  for reference

        if (localizedLookup) {
            int lastDot = name.lastIndexOf('.');
            String prefix = lastDot == -1 ? name : name.substring(0, lastDot);
            String suffix = lastDot == -1 ? "" : name.substring(lastDot);
            String localeName = LOCALE_SEPARATOR + locale.toString();
            StringBuffer buf = new StringBuffer(name.length() + localeName.length());
            buf.append(prefix);
            for (;;)
            {
                buf.setLength(prefix.length());
                String path = buf.append(localeName).append(suffix).toString();
                Object templateSource = acquireTemplateSource(path);

thanks
Ruben

------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and
AppDynamics. Performance Central is your source for news, insights,
analysis and resources for efficient Application Performance Management.
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Changing findTemplateSource behavior

Daniel Dekany
Wednesday, August 21, 2013, 11:23:04 AM, Ruben Trancoso wrote:

> Hello there,
>
> I started using freemarker in a project where I would like to find
> templates from database as well from filesystem.
>
> My expectation was to have a signature on TemplateLoader where I
> could get the template name and the localeName separetely so I could query the db.
>
> This requirement came from the idea that on the front end the user
> will add the name and the locale on colluns like this:
>
> some.template, en_US
> some.template, pt_BR
> some.template, pt_PT
> some.template, en
> ...
> some.other.template, jp_JP
>
> but findTemplateSource blend the name and locale and perform it's
> fallback strategy, and also expect that all files are suffixed with
> .ftl (or something) resulting in lookup keys like this:

findTemplateSource gets a deduced template name that already contains
the locale postfix, inserted before the file extension (if there was
any - it's not needed).

> some_en_US.template, some.other_pt_BR.template.
>
> My first Idea was to extend TemplateCache, but it should break
> compatibility with the TemplateLoader interface when it comes to be a File.
>
> So I was wondering if some experienced user/developer could put
> some light on how to achieve this without much pain and preserving the code structure.  .

I'm afraid you will have to extract this information from the template
name that findTemplateSource gets as parameter. For templates stored
on the file-system the convention is using .ftl file extension (helps
when you open them in an editor), and you probably want to keep it
transparent where the template comes from (file-system VS database).
Thus all template names should look the same, like some_en_US.ftl, and
then in the DataBaseTemplateLoader only, you will have to explode it
to "some" and Locale("en", "US"), then do the SELECT using that
extracted information. Removing the extension is trivial, but
extracting the locale information is tricky, as for that you will have
to assume that the file name doesn't use "_" for other purposes. If
you can't assume that, then it's indeed an ugly situation. (Anyway,
it's probable that TemplateLoader will have an extended version
sometimes later, then I will try to address this too. But that's in
the far and uncertain future, so...)

> //  for reference
>
>         if (localizedLookup) {
>             int lastDot = name.lastIndexOf('.');
>             String prefix = lastDot == -1 ? name : name.substring(0, lastDot);
>             String suffix = lastDot == -1 ? "" : name.substring(lastDot);
>             String localeName = LOCALE_SEPARATOR + locale.toString();
>             StringBuffer buf = new StringBuffer(name.length() + localeName.length());
>             buf.append(prefix);
>             for (;;)
>             {
>                 buf.setLength(prefix.length());
>                 String path =
> buf.append(localeName).append(suffix).toString();
>                 Object templateSource = acquireTemplateSource(path);
>
> thanks
> Ruben

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and
AppDynamics. Performance Central is your source for news, insights,
analysis and resources for efficient Application Performance Management.
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user