Discussion:
[FreeMarker-user] Changing findTemplateSource behavior
Ruben Trancoso
2013-08-21 09:23:04 UTC
Permalink
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
Daniel Dekany
2013-08-22 19:33:16 UTC
Permalink
Post by 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
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
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).
Post by Ruben Trancoso
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...)
Post by Ruben Trancoso
// 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
Loading...