Re: FreeMarker convert non-scalar to scalar

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Tuesday, August 31, 2010, 5:36:18 AM, Erin Drummond wrote:

> Hello,
>
> I have frequently found the need to convert Hash and Sequence objects
> to Scalars in my FreeMarker templates (for debugging purposes mainly,
> so I can see what data is being made available in the template). The
> scalar representation would be identical to how you define them, for
> example a Hash would convert to "{ "key" : "value" }" and a Sequence
> would convert to "[ "item1", "item2" ]". However, I have not been able
> to find such functionality in FreeMarker. I tried to create a custom
> Method to do it, but it just errors out saying that it expects a
> Scalar as a parameter only.

You should use TemplatMethodModelEx (or TemplateDirectiveModel if it
will output HTML). These accept all kind of parameters.

> How would I go about achieving this in FreeMarker? Would I have to try
> and create a "to_s" builtin (if so, where do I start?), or is there a
> way to create methods that can accept non-scalar arguments?

Yes there is: TemplateMethodModelEx.

Anyway, FreeMarker should have a someValue?dump built-in and a
<#dump someValue /> directive (the last would dump in XHTML). Implementing
these in a way that they work in most cases is more tricky than it
sounds though... once someone should sit down and do it however.

> Thanks,
> Erin

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
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.