Re: formating numeric string from variable

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Tuesday, January 12, 2010, 8:04:57 PM, lcp wrote:

> I am using Freemarker for a content.ftl file with Geoserver to be displayed
> in OpenLayers.
>
> I am not able to format my numeric values as strings. I can copy and paste
> the examples from the freemarker FAQ page into my script and they do work.
> These values are coming from a PostGIS database and are "numeric". 
>
> Could someone please tell me what they think I am doing wrong? I gave some
> examples of code I have tried.
>
> This is part of the error I get:
> Expected method. x?string evaluated instead to
> freemarker.template.SimpleScalar on line 17, column 3 in content.ftl. The
> problematic instruction: ---------- ==> ${x?string("0")} [on line 17, column
> 1 in content.ftl] ---------- Java backtrace for programmers: ----------
> freemarker.template.TemplateException: Expected method. x?string evaluated
> instead to freemarker.template.SimpleScalar on line 17, column 3 in
> content.ftl. at...

I know you said they are numbers, but it still looks like a problem
with the type of x. Because, x?string produces a method when x was a
number or a date or a boolean, but not when x is a string; then it
just returns a string that you can't call, hence the above error.
Could you use ?is_number?string in place of ?string.whatever and
?string(whatever) at the failing locations, to see if there are any
"false"-s? If there are only "true"-s, could you try the same with
?is_string?string to see if there are any "true"-s (in which case you
have some multi-typed values... that possibly can cause issues
depending on which type ?string gives the priority (which I don't know
right now...).)

> Thank you, LP
>
> DOESN'T WORK
> <#if attribute.name = "last">
>       <li>% of people living on less than $1 per day PPP - most recent
> measurement of this indicator between 2002 and
> 2007:${attribute.value?string.currency}</li>
>    </#if>
>   </#list>
> -----------------
> DOESN'T WORK
> ${attribute.value?string(",##0.00")}</li>
> -----------------
> DOESN'T WORK
> ${attribute.value}</li>
> <#assign x = attribute.value>
> ${x?string("0")}
> ------------------
> DOES WORK

That's scary... because attribute.value?whatever meant to be
equivalent with x?whatever after <#assign x = attribute.value>
(unless, of course, you have a #local x that hides it). If it
doesn't... well, I don't think such a serious bug could survive for so
long.

> ${attribute.value}</li>
>  <#assign x = attribute.value>
> ${x?string}  <#-- the same as ${x} -->

${attribute.value?string} should work then too...

> ------------------
> DOES WORK
>
> ${attribute.value}</li>
> <#assign x = 1.234>
> ${x?string("0")}
> -----------------

... and also ${attribute.value?string("0")} should work then.

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
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.