Re: numeric value replacement

"Jonathan Revusky" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
On Thu, Jun 5, 2008 at 11:34 PM, Samy123 <[email protected]> wrote:
>
> Thanks for reply Jonathan..!
> I tried your solution and it works !!!
> It works even when my hashkey is not a string ,

But it is a string!

> I am passing hashkey as
> input.put ("101", "blah blah");
>
> Does this mean .vars is considering the value passed as string ?

"101" is a string and 101 is a number.

When you say .vars["name"] it is a longer way of saying just name
basically, but it has the advantage that the thing in quotes, "name"
can be a string that doesn't work as a plain variable, like "101",
since if you just put 101 in the template, it thinks that is a number,
not a variable.

Well, also .vars gives you a way of constructing variable names, like
<#list 1..3 as i>
   ${.vars["a" + i]}
</#list>

is the same effectively as:
  ${a1}
  ${a2}
  ${a3}

.vars is useful and worth knowing about. But is more useful still to
know the difference between a string and a number. :-)


JR


>
>
>
>
>
> Jonathan Revusky-3 wrote:
>>
>> On Thu, Jun 5, 2008 at 8:42 PM, Samy123 <[email protected]> wrote:
>>>
>>> Sorry for confusion...here is the code snippet
>>>
>>> String s="<#if  StateCode?? && StateCOde == 'FL' > The rent information
>>> is
>>> as follows ${101}  </#if>";
>>> StringBuffer sbuf=new StringBuffer();
>>> sbuf.append(s);
>>>
>>> Map input = new HashMap();
>>>    input.put("stateCode", "FL");
>>>    input.put("101", " $199.99 per year..state charges will be levied as
>>> per
>>> the aggreement"); //new Integer(101).intValue() ?
>>>
>>> Template template = new Template("stringTemplate", new
>>> StringReader(sbuf.toString()), new Configuration());
>>>
>>> StringWriter sw = new StringWriter();
>>>        template.process(input, sw);
>>>
>>>  System.out.println("===="+sw.toString());
>>>
>>> I am not getting any error on this,but in log i am getting 101 printed
>>> instead of the actual value of 101.
>>
>> Variable names in FreeMarker are basically like variable names in Java
>> (with a minor difference or two) so you can't have a variable name
>> that is a number.
>>
>> Now, you could get at a variable that has a key that is numerical by
>> writing:
>>
>> ${.vars["101"]}
>>
>> instead of ${101}
>>
>> but even then you still need for the hash key to be a string, i.e.
>>
>> input.put ("" + 101, "blah blah");
>>
>>
>>
>> Use ${.vars[
>>
>>> Hope it helps.
>>>
>>>
>>> Thanks,
>>>
>>>
>>>
>>>
>>>
>>>
>>> ========
>>>
>>> Attila Szegedi-3 wrote:
>>>>
>>>> Yeah - I'm sorry, but I can't understand the problem either. How about
>>>> you show us a snippet of template code, show us what output (or error)
>>>> it produces, and describe what would you expect instead?
>>>>
>>>> Attila.
>>>>
>>>> On 2008.06.05., at 10:43, Daniel Dekany wrote:
>>>>
>>>>> Thursday, June 5, 2008, 7:12:08 AM, Samy123 wrote:
>>>>>
>>>>>> Hi-
>>>>>> Is there any way to replace value of a number with the desired
>>>>>> value..
>>>>>> for ex. I am having a following line in the template
>>>>>> The condition is ${120}.
>>>>>>
>>>>>> And the value of 120 is  stored in the string as "for number a
>>>>>> specific
>>>>>> number".
>>>>>> Now how can I replace the value of 120 with the string?
>>>>>> As of now while running the freemarker,it is printing the number
>>>>>> 120 as it
>>>>>> is and not replacing with the string.
>>>>>
>>>>> You may need to rephrase this whole question because certainly nobody
>>>>> will understand it. I don't understand it at least. As ${aNumber} will
>>>>> be replaced in the output with a character sequence, the value of
>>>>> aNumber will be converted to string without asking. If you want to
>>>>> convert a number to a string inside a more complex expression, like in
>>>>> ${iNeedAString(aNumber)}, then you should write aNumber?c or
>>>>> aNumber?string, depending on whether you want "computer audience" or
>>>>> "human audience" formatting. See:
>>>>> http://freemarker.org/docs/ref_builtins_number.html
>>>>>
>>>>>> (tested on 2.1.12 of freemarker).
>>>>>>
>>>>>> Thanks,
>>>>>> ~Samir
>>>>
>>>> -------------------------------------------------------------------------
>>>> Check out the new SourceForge.net Marketplace.
>>>> It's the best place to buy or sell services for
>>>> just about anything Open Source.
>>>> http://sourceforge.net/services/buy/index.php
>>>> _______________________________________________
>>>> FreeMarker-user mailing list
>>>> [email protected]
>>>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/numeric-value-replacement-tp17662266p17669929.html
>>> Sent from the freemarker-user mailing list archive at Nabble.com.
>>>
>>>
>>> -------------------------------------------------------------------------
>>> Check out the new SourceForge.net Marketplace.
>>> It's the best place to buy or sell services for
>>> just about anything Open Source.
>>> http://sourceforge.net/services/buy/index.php
>>> _______________________________________________
>>> FreeMarker-user mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>>>
>>
>> -------------------------------------------------------------------------
>> Check out the new SourceForge.net Marketplace.
>> It's the best place to buy or sell services for
>> just about anything Open Source.
>> http://sourceforge.net/services/buy/index.php
>> _______________________________________________
>> FreeMarker-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>>
>>
>
> --
> View this message in context: http://www.nabble.com/numeric-value-replacement-tp17662266p17680015.html
> Sent from the freemarker-user mailing list archive at Nabble.com.
>
>
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://sourceforge.net/services/buy/index.php
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
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.