Re: Assign the bean value to a variable defined in template

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, January 18, 2008, 4:37:15 PM, Samuel Viana wrote:

> Hi
>
> I'm new to FreeMarker and I am trying to capture the content of a bean to a
> variable defined in the template. The purpose of this is to give the designers
> the opportunity to easily create the templates providing only a macro that calls
> that variable and renders a table row with the bean's contents, contents that
> are holded in a variable that is human-readable, the problem is that the bean's
> is passed through an attribute that comes from a Struts action - truthly
> speaking, the bean is stored in an HashMap that is acceded in this way:
>
> <#assign
> lavazza2200>content.getProduct("106738243-20000212")</#assign>
>
> "content" is the map that cames from the Structs Action class, and in 
> here the argument of the getProduct method is the key to fetch
> the bean, that should be stored in the variable named "lavazza20".
> This way, the web designers only need to do:
>
> <@table_row prod="lavazza2200"/>

That should be <@table_row prod=lavazza2200 />. Like in JavaScript,
you must not use quotation marks around a variable reference.

> in order to a draw a table row with the bean's contents.
>
> The problem is that what the variable "lavazza2200" really holds is not the bean
> itself, but rather the output of its toString method. Anyway, I was forced to
> use the syntax
>
>  <#assign [variablename]contents_for_the_variable></#assign>

(That does output capturing, so you will get the result formatted to
text, as you have noted.)

> since this syntax is not allowed:
>
> <#assign lavazza2200 = ${content.getProduct("106738243-20000212")} />

The correct syntax is simply:

  <#assign lavazza2200 = content.getProduct("106738243-20000212")>

That is, no ${} is needed or allowed there. Inside <#...> and ${...}
the basic approach of the syntax is similar to an usual programming
language syntax (like JavaScript), while outside them you have static
text.

> Can you suggest me a way allowed by Freemarker to store the bean inside a
> variable that its defined in the template ? Remember that what the Struts Action
> is sending to the template is a Map, that contains the bean that I want to store
> in a certain variable.
>
> Best regards,
> Samuel

-- 
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
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.