You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Kent Johnson <ke...@tds.net> on 2005/04/06 22:18:48 UTC

Jython and Velocity

I'm trying to use Jython objects in a Velocity template. I have a start of an Uberspect that 
supports iteration. It is similar to the one Jason Briggs posted to the Wiki though his is more 
complete.

The problem I am having is accessing individual elements of Jython lists. To do this with a method 
call I have to call the __getitem__() method of the list. For some reason the Velocity parser does 
not recognize method names that start with underscore so this doesn't work.

So, the questions:
- Why doesn't Velocity recognize method names starting with underscore? They are valid Java names.
- Can anyone suggest a workaround? (Jason, have you figured this out?)

Thanks,
Kent


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


Re: Jython and Velocity

Posted by Kent Johnson <ke...@tds.net>.
I think I'll just keep creating ArrayLists and HashMaps when I need to put collections into Velocity 
contexts. And wait for Jython 2.2 in which it looks like Jython list and dict will implement 
java.util.List and java.util.Map.

Kent

Jason R Briggs wrote:
> The listtool idea does work.  I've updated my JythonUberspect in the
> wiki accordingly, but to reiterate (assuming you're developing jython
> servlets):
> 
> Create a jython class as follows:
> 
> class listtool:
>     def get(self, obj, idx):
>         if type(obj) in (types.TupleType, types.ListType) and idx <
> len(obj):
>             return obj[idx]
>         else:
>             return None
> 
> in your jython servlet add the tool to the context:
> 
> ctx.put('listtool', listtool())
> 
> then in your template you can go:
> 
> $listtool.get($mylist, 5)
> 
> Which seems to work for me.  Let me know how you get on.
> 
> Regards
> Jason
> 
> Jason R Briggs wrote:
> 
> 
>>Forgot to mention, the other thought is just a modified list tool that
>>knows how to handle jython sequences.  (i.e. something like
>>$listtool.get($jythonlist, 3) )  Not as nice as accessing the object
>>directly perhaps, but arguably a better approach that the hack I mentioned.
>>
>>Jason R Briggs wrote:
>>
>> 
>>
>>
>>>Do you mean accessing a list element via index?  In which case, no I
>>>haven't figured it out. 
>>>
>>>It looks like velocity doesn't like anything starting with an
>>>underscore.  The docs do say something along the lines that a valid VTL
>>>identifier must start with an alphabetic character, but I would have
>>>thought a method name might have been handled differently.
>>>
>>>The only idea I've come up with so far is to hack a method into the
>>>uberspect that mimicks a java ArrayList.get(idx).  i.e. if the object is
>>>a jython sequence, and the velocity reference is something like
>>>$list.get($element), then perform a  __getattr__ in the uberspect. 
>>>Seems hideously hacky though, so I hadn't gone any further than that.
>>>
>>>J
>>>
>>>Kent Johnson wrote:
>>>
>>>
>>>
>>>   
>>>
>>>
>>>>I'm trying to use Jython objects in a Velocity template. I have a
>>>>start of an Uberspect that supports iteration. It is similar to the
>>>>one Jason Briggs posted to the Wiki though his is more complete.
>>>>
>>>>The problem I am having is accessing individual elements of Jython
>>>>lists. To do this with a method call I have to call the __getitem__()
>>>>method of the list. For some reason the Velocity parser does not
>>>>recognize method names that start with underscore so this doesn't work.
>>>>
>>>>So, the questions:
>>>>- Why doesn't Velocity recognize method names starting with
>>>>underscore? They are valid Java names.
>>>>- Can anyone suggest a workaround? (Jason, have you figured this out?)
>>>>
>>>>Thanks,
>>>>Kent
>>>>
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>>>>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>>>
>>>>
>>>>  
>>>>
>>>>     
>>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>>
>>>
>>>
>>>
>>>   
>>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>
>>
>> 
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 
> 


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


Re: Jython and Velocity

Posted by Jason R Briggs <ja...@gmail.com>.
The listtool idea does work.  I've updated my JythonUberspect in the
wiki accordingly, but to reiterate (assuming you're developing jython
servlets):

Create a jython class as follows:

class listtool:
    def get(self, obj, idx):
        if type(obj) in (types.TupleType, types.ListType) and idx <
len(obj):
            return obj[idx]
        else:
            return None

in your jython servlet add the tool to the context:

ctx.put('listtool', listtool())

then in your template you can go:

$listtool.get($mylist, 5)

Which seems to work for me.  Let me know how you get on.

Regards
Jason

Jason R Briggs wrote:

>Forgot to mention, the other thought is just a modified list tool that
>knows how to handle jython sequences.  (i.e. something like
>$listtool.get($jythonlist, 3) )  Not as nice as accessing the object
>directly perhaps, but arguably a better approach that the hack I mentioned.
>
>Jason R Briggs wrote:
>
>  
>
>>Do you mean accessing a list element via index?  In which case, no I
>>haven't figured it out. 
>>
>>It looks like velocity doesn't like anything starting with an
>>underscore.  The docs do say something along the lines that a valid VTL
>>identifier must start with an alphabetic character, but I would have
>>thought a method name might have been handled differently.
>>
>>The only idea I've come up with so far is to hack a method into the
>>uberspect that mimicks a java ArrayList.get(idx).  i.e. if the object is
>>a jython sequence, and the velocity reference is something like
>>$list.get($element), then perform a  __getattr__ in the uberspect. 
>>Seems hideously hacky though, so I hadn't gone any further than that.
>>
>>J
>>
>>Kent Johnson wrote:
>>
>> 
>>
>>    
>>
>>>I'm trying to use Jython objects in a Velocity template. I have a
>>>start of an Uberspect that supports iteration. It is similar to the
>>>one Jason Briggs posted to the Wiki though his is more complete.
>>>
>>>The problem I am having is accessing individual elements of Jython
>>>lists. To do this with a method call I have to call the __getitem__()
>>>method of the list. For some reason the Velocity parser does not
>>>recognize method names that start with underscore so this doesn't work.
>>>
>>>So, the questions:
>>>- Why doesn't Velocity recognize method names starting with
>>>underscore? They are valid Java names.
>>>- Can anyone suggest a workaround? (Jason, have you figured this out?)
>>>
>>>Thanks,
>>>Kent
>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>>
>>>
>>>   
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>
>>
>> 
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>
>  
>


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


Re: Jython and Velocity

Posted by Jason R Briggs <ja...@gmail.com>.
Forgot to mention, the other thought is just a modified list tool that
knows how to handle jython sequences.  (i.e. something like
$listtool.get($jythonlist, 3) )  Not as nice as accessing the object
directly perhaps, but arguably a better approach that the hack I mentioned.

Jason R Briggs wrote:

>Do you mean accessing a list element via index?  In which case, no I
>haven't figured it out. 
>
>It looks like velocity doesn't like anything starting with an
>underscore.  The docs do say something along the lines that a valid VTL
>identifier must start with an alphabetic character, but I would have
>thought a method name might have been handled differently.
>
>The only idea I've come up with so far is to hack a method into the
>uberspect that mimicks a java ArrayList.get(idx).  i.e. if the object is
>a jython sequence, and the velocity reference is something like
>$list.get($element), then perform a  __getattr__ in the uberspect. 
>Seems hideously hacky though, so I hadn't gone any further than that.
>
>J
>
>Kent Johnson wrote:
>
>  
>
>>I'm trying to use Jython objects in a Velocity template. I have a
>>start of an Uberspect that supports iteration. It is similar to the
>>one Jason Briggs posted to the Wiki though his is more complete.
>>
>>The problem I am having is accessing individual elements of Jython
>>lists. To do this with a method call I have to call the __getitem__()
>>method of the list. For some reason the Velocity parser does not
>>recognize method names that start with underscore so this doesn't work.
>>
>>So, the questions:
>>- Why doesn't Velocity recognize method names starting with
>>underscore? They are valid Java names.
>>- Can anyone suggest a workaround? (Jason, have you figured this out?)
>>
>>Thanks,
>>Kent
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>>
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>
>  
>


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


Re: Jython and Velocity

Posted by Jason R Briggs <ja...@gmail.com>.
Do you mean accessing a list element via index?  In which case, no I
haven't figured it out. 

It looks like velocity doesn't like anything starting with an
underscore.  The docs do say something along the lines that a valid VTL
identifier must start with an alphabetic character, but I would have
thought a method name might have been handled differently.

The only idea I've come up with so far is to hack a method into the
uberspect that mimicks a java ArrayList.get(idx).  i.e. if the object is
a jython sequence, and the velocity reference is something like
$list.get($element), then perform a  __getattr__ in the uberspect. 
Seems hideously hacky though, so I hadn't gone any further than that.

J

Kent Johnson wrote:

> I'm trying to use Jython objects in a Velocity template. I have a
> start of an Uberspect that supports iteration. It is similar to the
> one Jason Briggs posted to the Wiki though his is more complete.
>
> The problem I am having is accessing individual elements of Jython
> lists. To do this with a method call I have to call the __getitem__()
> method of the list. For some reason the Velocity parser does not
> recognize method names that start with underscore so this doesn't work.
>
> So, the questions:
> - Why doesn't Velocity recognize method names starting with
> underscore? They are valid Java names.
> - Can anyone suggest a workaround? (Jason, have you figured this out?)
>
> Thanks,
> Kent
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
>


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