Re: How to add custom method using built-in-like syntax?

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, December 9, 2010, 11:56:37 PM, Chris wrote:

> I'd like to add my own functionality to templates using syntax which is
> similar to that for built-ins:
>
> Here's a translation:
> ${mytext?translate_into_swahili}
>
> Is this possible?

No, the ?something syntax is reserved for core functions to avoid name
clashes when new core functions are added. You can do
${translate_into_swahili(mytext)} however. For that, depending on your
application, do one of these:

- Create a template that defines your function(s) then include
  #include or #import that in your normal templates. Note that
  functions can be implemented in pure FreeMarker with #function, or
  in Java as TemplateMethodModelEx-s and then where you would use
  <#function foo>...</#function> use
  <#assign foo=your.class.Name"?new()> instead.

- Implement the function in Java as TemplateMethodModelEx, and the put
  it into the data-model

- Implement the function in Java as TemplateMethodModelEx, and then
  make it available on the single spot where you initialize FreeMarker with
  Configuration.setSharedVariable(name, method)

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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.