You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Thomas Peter Berntsen <no...@translucent.dk> on 2006/03/27 16:45:41 UTC

Dynamically generated values

Hi guys

I'm using velocity to generate a 5x5 matrix with each entry having its 
own id like this:

<table style="border:1px solid black;">z
		*#foreach* ($row in $rows)
		<tr>
			*#foreach* ($col in $cols)
			*#set*( $product = $col * $row )
			*#set*( $id = "$col$row" )
			<td>
				Row: $row, Column: $col <br/>
				Number: $id
			</td>
			*#end*
		</tr>
		*#end*
	</table>

The template gets $rows and $cols, two int[]s containing values 1 through 5.

The $id is a string concatenation of $col and $row.

I am passing a key-value pairs - eg. ("entry_15", "FOO") to the template 
as well, where the key corresponds to the entry in the matrix in which 
the value should be shown.

How do I accomplish this?

I know that I would normally address the key to get the value like 
$entry_15, but the "15" is the dynamically generated value from the 
Velocity template itself (what is $id in the example above)... So I need 
something like $entry_#generateValue where" #generateValue" is a macro 
which precedes the "$entry_".

I've tried a lot of things, but it's all erraneous.

I look forward to hearing from you.

Cheers,
Thomas





Re: Dynamically generated values

Posted by Thomas Peter Berntsen <no...@translucent.dk>.
Hi Claude

Thank you very much for your help.

I turned to solution 1. as I can't count on RenderTools being available 
in the target environment.

Cheers,
Thomas

Claude Brisson wrote:
> Sorry.
>
> You should read
> 	$render.evaluate($context,"\$entry_$id")
> and
> 	$render.evaluate("\$entry_$id")
>
>   Claude
>
>   
>>         if not, then use the RenderTool; you will also need $context as
>>         in 2
>> 	$render.evaluate($context,"entry_$id")
>>
>>     
>
>
> Le lundi 27 mars 2006 à 18:50 +0200, Claude Brisson a écrit :
>   
>> Hi.
>>
>> There are several solutions.
>>
>> 1. You can put the key/value pairs in a map that you put in the context:
>> 	$catalog.get("entry_$id")
>>
>> 2. You can put the context inside itself
>> 	$context.get("entry_$id")
>>
>> 3. A more generic solution is to use a render tool from the
>> velocity-tools subproject.
>>    
>>         If you are using the VelocityViewServlet, you can use the
>>         ViewRenderTool :
>>         	$render.evaluate("$entry_$id")
>>
>>         if not, then use the RenderTool; you will also need $context as
>>         in 2
>> 	$render.evaluate($context,"entry_$id")
>>
>>   --
>>   Claude
>>
>> Le lundi 27 mars 2006 à 16:45 +0200, Thomas Peter Berntsen a écrit :
>>     
>>> Hi guys
>>>
>>> I'm using velocity to generate a 5x5 matrix with each entry having its 
>>> own id like this:
>>>
>>> <table style="border:1px solid black;">z
>>> 		*#foreach* ($row in $rows)
>>> 		<tr>
>>> 			*#foreach* ($col in $cols)
>>> 			*#set*( $product = $col * $row )
>>> 			*#set*( $id = "$col$row" )
>>> 			<td>
>>> 				Row: $row, Column: $col <br/>
>>> 				Number: $id
>>> 			</td>
>>> 			*#end*
>>> 		</tr>
>>> 		*#end*
>>> 	</table>
>>>
>>> The template gets $rows and $cols, two int[]s containing values 1 through 5.
>>>
>>> The $id is a string concatenation of $col and $row.
>>>
>>> I am passing a key-value pairs - eg. ("entry_15", "FOO") to the template 
>>> as well, where the key corresponds to the entry in the matrix in which 
>>> the value should be shown.
>>>
>>> How do I accomplish this?
>>>
>>> I know that I would normally address the key to get the value like 
>>> $entry_15, but the "15" is the dynamically generated value from the 
>>> Velocity template itself (what is $id in the example above)... So I need 
>>> something like $entry_#generateValue where" #generateValue" is a macro 
>>> which precedes the "$entry_".
>>>
>>> I've tried a lot of things, but it's all erraneous.
>>>
>>> I look forward to hearing from you.
>>>
>>> Cheers,
>>> Thomas
>>>
>>>
>>>
>>>
>>>       
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>>     
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>   

Re: Dynamically generated values

Posted by Claude Brisson <cl...@renegat.net>.
Sorry.

You should read
	$render.evaluate($context,"\$entry_$id")
and
	$render.evaluate("\$entry_$id")

  Claude

> 
>         if not, then use the RenderTool; you will also need $context as
>         in 2
> 	$render.evaluate($context,"entry_$id")
> 


Le lundi 27 mars 2006 à 18:50 +0200, Claude Brisson a écrit :
> Hi.
> 
> There are several solutions.
> 
> 1. You can put the key/value pairs in a map that you put in the context:
> 	$catalog.get("entry_$id")
> 
> 2. You can put the context inside itself
> 	$context.get("entry_$id")
> 
> 3. A more generic solution is to use a render tool from the
> velocity-tools subproject.
>    
>         If you are using the VelocityViewServlet, you can use the
>         ViewRenderTool :
>         	$render.evaluate("$entry_$id")
> 
>         if not, then use the RenderTool; you will also need $context as
>         in 2
> 	$render.evaluate($context,"entry_$id")
> 
>   --
>   Claude
> 
> Le lundi 27 mars 2006 à 16:45 +0200, Thomas Peter Berntsen a écrit :
> > Hi guys
> > 
> > I'm using velocity to generate a 5x5 matrix with each entry having its 
> > own id like this:
> > 
> > <table style="border:1px solid black;">z
> > 		*#foreach* ($row in $rows)
> > 		<tr>
> > 			*#foreach* ($col in $cols)
> > 			*#set*( $product = $col * $row )
> > 			*#set*( $id = "$col$row" )
> > 			<td>
> > 				Row: $row, Column: $col <br/>
> > 				Number: $id
> > 			</td>
> > 			*#end*
> > 		</tr>
> > 		*#end*
> > 	</table>
> > 
> > The template gets $rows and $cols, two int[]s containing values 1 through 5.
> > 
> > The $id is a string concatenation of $col and $row.
> > 
> > I am passing a key-value pairs - eg. ("entry_15", "FOO") to the template 
> > as well, where the key corresponds to the entry in the matrix in which 
> > the value should be shown.
> > 
> > How do I accomplish this?
> > 
> > I know that I would normally address the key to get the value like 
> > $entry_15, but the "15" is the dynamically generated value from the 
> > Velocity template itself (what is $id in the example above)... So I need 
> > something like $entry_#generateValue where" #generateValue" is a macro 
> > which precedes the "$entry_".
> > 
> > I've tried a lot of things, but it's all erraneous.
> > 
> > I look forward to hearing from you.
> > 
> > Cheers,
> > Thomas
> > 
> > 
> > 
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Dynamically generated values

Posted by Claude Brisson <cl...@renegat.net>.
Hi.

There are several solutions.

1. You can put the key/value pairs in a map that you put in the context:
	$catalog.get("entry_$id")

2. You can put the context inside itself
	$context.get("entry_$id")

3. A more generic solution is to use a render tool from the
velocity-tools subproject.
   
        If you are using the VelocityViewServlet, you can use the
        ViewRenderTool :
        	$render.evaluate("$entry_$id")

        if not, then use the RenderTool; you will also need $context as
        in 2
	$render.evaluate($context,"entry_$id")

  --
  Claude

Le lundi 27 mars 2006 à 16:45 +0200, Thomas Peter Berntsen a écrit :
> Hi guys
> 
> I'm using velocity to generate a 5x5 matrix with each entry having its 
> own id like this:
> 
> <table style="border:1px solid black;">z
> 		*#foreach* ($row in $rows)
> 		<tr>
> 			*#foreach* ($col in $cols)
> 			*#set*( $product = $col * $row )
> 			*#set*( $id = "$col$row" )
> 			<td>
> 				Row: $row, Column: $col <br/>
> 				Number: $id
> 			</td>
> 			*#end*
> 		</tr>
> 		*#end*
> 	</table>
> 
> The template gets $rows and $cols, two int[]s containing values 1 through 5.
> 
> The $id is a string concatenation of $col and $row.
> 
> I am passing a key-value pairs - eg. ("entry_15", "FOO") to the template 
> as well, where the key corresponds to the entry in the matrix in which 
> the value should be shown.
> 
> How do I accomplish this?
> 
> I know that I would normally address the key to get the value like 
> $entry_15, but the "15" is the dynamically generated value from the 
> Velocity template itself (what is $id in the example above)... So I need 
> something like $entry_#generateValue where" #generateValue" is a macro 
> which precedes the "$entry_".
> 
> I've tried a lot of things, but it's all erraneous.
> 
> I look forward to hearing from you.
> 
> Cheers,
> Thomas
> 
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: ternary operator

Posted by Will Glass-Husain <wg...@forio.com>.
I don't think this is supported.

On 3/28/06, Michael Fortin <mi...@vendorpromotions.com> wrote:
> Hello,
> Is it possible to use the ternary operator in a set directive? I tried
> the fallowing set directive but it didn't work,  is there an alternative
> way to do this without and if else statement?
>
> #set($test = $command == null ? 'its null' : 'not null')
> $test
>
> thanks,
> Michael
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


--
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


ternary operator

Posted by Michael Fortin <mi...@vendorpromotions.com>.
Hello,
Is it possible to use the ternary operator in a set directive? I tried 
the fallowing set directive but it didn't work,  is there an alternative 
way to do this without and if else statement?

#set($test = $command == null ? 'its null' : 'not null')
$test

thanks,
Michael



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org