Re: someVar to SOME_VAR

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Saturday, February 14, 2009, 10:32:20 PM, Alex7 wrote:
>
> Sure, i wrap it, but razer to macro then function:
>
> <#macro ConvertToConstantName variable><#compress>
> ${variable?replace('(\\p{Upper})', '_$1', 'r')?upper_case}
> </#compress></#macro>
>
>
> Actually i don't know other difference between macro and function beside
> that function must have return statement.

That's a quite confusing aspect of FM indeed... But let me copy-paste
from the Manual:

  Methods and functions
  ---------------------

  A value that is a method or a function is used to calculate another
  value, influenced by the parameters you give to it.

  [...]

  What is the difference between a method and a function? As far as
  the template author is concerned, nothing. Well not really nothing,
  as methods typically come from the data-model (as they reflect the
  methods of Java objects), and functions are defined in templates
  (with the "function" directive -- an advanced topic), but both can
  be used on the same way.


  User-defined directives
  -----------------------

  A value of this type can be used as user-defined directive (with
  other words, as FreeMarker tag). An user-defined directive is a
  subroutine, something like a little reusable template fragment.

  [...]


  Function/method versus user-defined directive   <--- READ THIS!
  ---------------------------------------------

  [...] It's a frequent dilemma if you should use a function/method or
  an user-defined directive to implement something. The rule of thumb
  is: Implement the facility as user-defined directive instead of as
  function/method if:

  * ... the output (the return value) is markup (HTML, XML, etc.). The
    main reason is that the result of functions are subject to
    automatic XML-escaping (due to the nature of ${...}), while the
    output of user-defined directives are not (due to the nature of
    <@...>; its output is assumed to be markup, and hence already
    escaped).

  * ... it's the side-effect that is important and not the return
    value. For example, a directive whose purpose is to add an entry
    to the server log is like that. (In fact you can't have a return
    value for a user-defined directive, but some kind of feedback is
    still possible by setting non-local variables.)

  * ... it will do flow control (like for example list or if
    directives do). You just can't do that with a function/method
    anyway.

The Java methods of FreeMarker-unaware Java objects are normally
visible as methods in templates, regardless of the nature of the Java
method. That said, you have no choice there.

> Neither macro no function could be used inside tag AFAIK.

Functions can be used at the same places in the template as methods
can. So for example, you can write <@yourMethod yourFunction() />, but
not <@yourMethod <@yourOtherMethod /> />.

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
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.