Re: #visit / #recurse with alternative macros / display formats

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, June 7, 2012, 3:11:08 PM, Eirik Lygre wrote:

> We have a couple of scenarios where we use the recursive templating support,
> with #visit or #recurse automatically calling a macro named @Dimensions.
>
> Now, sometimes we want to / need to use the same template for multiple layouts.
> However, since the macro name is determined automatically, we need something
> smart.
>
> 1) Is there an obvious solution that we haven't seen?
>
> We were thinking of using namespaces. However, we cannot refer to external
> templates, so we'd need to create macros in different namespaces from the same
> template, which doesn't seem possible:
>
> [#if format == 'full']
>    [#visit dimensions using full]
> [#else]
>    [#visit dimensions]
> [#/if]

Just in case you haven't realized, you don't need if/else-s around
each #visit/#recurse calls. You could just do this at the beginning of
the tempalte:

 [#if format == 'full']
    [#assign ns = full]
 [#else]
    [#visit ns = .namespace]
 [#/if]

and then later:

  [#visit dimensions using ns]

Note that ns also can be sequence of namespaces, so you could do
something like:

  [#visit ns = [.namespace, extensions]]

> [#macro @Dimension]        [#-- Default namespace --]
> ...
> [/#macro]
>
> [#macro @full.Dimension]   [#-- Use namespace "full" --]
> ...
> [/#macro]
>
> 2) Is this possible?

Nope... The problem is that the 2nd parameter to #visit/#recurse is a
real namespace (or a sequence of those), not just map that you could
generate freely.

> Should it be?

As far as I understand, you need this because you can't refer
(#import) to other templates. That's a quite severe constraint.
Addressing that sounds like the real solution... if that's possible.

> Or, any other suggestions?
>
> Eirik

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
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.