[stringtemplate-interest] How to use variables inside formatmodifiers

Koerner, Ron ron.koerner at wincor-nixdorf.com
Mon Aug 3 02:36:13 PDT 2009


Hi Mohamed,
 
actually it is the philosophy of StringTemplate that templates don't do
calculations other than what is needed for display. So I fear it is not
the right tool for your task, since you require something which builds a
page model (all data displayed on the generated page) from your business
model (all data the application provides) and something which generates
a visible page from the page model.
StringTemplate only performs the latter job. If you want to use ST
anyway you need to build something which takes some userdefined
rules/calculations and generates a page model out of the business model.
 
If you prefer a very dirty hack: create an object like:
 
class Calculator implements Map { public String get(String expr) {
return eval(expr); }  /* implement dummies for the other operations */ }
 
Add a calculator to your page model:
st.setAttribute("calc",new Calculator());
 
Use the calculator in your template:
 
$calc.({$a$+$b$})$
 
Just to make clear: this is a very very dirty hack and violates the
StringTemplate philosophy in ways which could bring the wrath of the
god(s) of model/view-separation upon you or me and may or may not turn
the one who actually implements the above into an appropriate heap of
dust.
Also Terence might not invite you to dinner anymore...
 
Regards,
Ron
 
________________________________

From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of Mohamed
Mansour
Sent: Friday, July 31, 2009 8:27 PM
To: Jim Idle
Cc: stringtemplate-interest at antlr.org
Subject: Re: [stringtemplate-interest] How to use variables inside
formatmodifiers


Hmm, that would be unfortunate. I can't use this kind of approach then.
I am trying to create a generic "Report" where every user can do
whatever they want with it. If they wanted to add 10 to the result to
any variable, they could. But we can't do that with StringTemplate. 

I guess that is the limitation of StringTemplate.

Isn't there any way I can make it work, like a hack? A user can modify a
variable by appending(adding) a number to it. (simply scenario)

Thanks,

-- Mohamed Mansour



On Fri, Jul 31, 2009 at 2:16 PM, Jim Idle <jimi at temporal-wave.com>
wrote:


	Mohamed Mansour wrote: 

		Hello, 

		I have been using StringTemplate to produce stylized
report files. Some variables need to be formatted someway, for example
if I want to uppercase a variable I do, therefore I setup an attribute
renderer:
		$foo;format="uppercase"$

		In my specific case I would need to apply more than one
formatter (more than one attribute renderer for a variable), so I
created many attribute renders and I can process them like:
		$foo;format="uppercase|fixedlength:10:x:R|lowercase"$ If
you notice a modifer could have parameters ( with colons as seen with
fixedlength)

		So if $foo is "123aBc" it will return "XXXX123abc", the
above works perfectly fine

		My main problem is that, some parameters in my format
modifiers need to be ST variables, for example, I would need to add a
number:
		$numA;format="calculate:+:$numB$"$

		If numA = 10, and numB = 5, the formatter will return15.
But my problem is that, it treats the variables as a string doesn't
actually give me the exact value of the variable.

		How can I pass variables in the "format" area?

	Calculations need to be performed before setting the values in
the templates otherwise you are breaking the model/view separation that
StringTemplate is as strict about as it can be. In other words you
should not be trying to do this.
	
	The attribute renderer while powerful, has to be used carefully
so that you do not break this  separation as it is kind of a back door
(the only way to close it would be to implement a generic formatting
engine and get rid of attribute renderers, but that is a lot of code
bloat of course.
	
	Jim
	

	_______________________________________________
	stringtemplate-interest mailing list
	stringtemplate-interest at antlr.org
	http://www.antlr.org/mailman/listinfo/stringtemplate-interest
	
	



-- 
WINCOR NIXDORF International GmbH 
Sitz der Gesellschaft: Paderborn 
Registergericht Paderborn HRB 3507
Geschäftsführer: Eckard Heidloff (Vorsitzender), Stefan Auerbach, Dr. Jürgen Wunram
Vorsitzender des Aufsichtsrats: Karl-Heinz Stiller 
Steuernummer: 339/5884/0020 - Ust-ID Nr.: DE812927716 - WEEE-Reg.-Nr. DE44477193

Diese E-Mail enthält vertrauliche Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.

This e-mail may contain confidential information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20090803/05e53980/attachment.html 


More information about the stringtemplate-interest mailing list