Anyone tried to use Velocity with scala/lift?

95 views
Skip to first unread message

Ewan

unread,
Jul 9, 2009, 12:24:28 PM7/9/09
to Lift
All

I am trying to create an email using a velocity template all wired up
with Apache Camel. The camel stuff aside I can't seem to get velocity
to render Scala lists along the lines of:

#foreach($email in $headers.emailAddresses)
hello $email
#end

where headers is a map and emailAddresses is a list. Strings in the
headers map works fine i.e. ${headers.sender}.

If I dump out the contents of headers using ${headers} the toString
for the map renders as expected.

I have not delved into the velocity code but I wonder if tries to
create a java interator over the list which does not work in this case
- maybe?

--Ewan

Naftoli Gugenhem

unread,
Jul 9, 2009, 12:40:49 PM7/9/09
to eha...@gmail.com, lif...@googlegroups.com
I don't know, but I would be surprised if it didn't!
Can you give it a Java Iterable view?

-------------------------------------

Ewan

unread,
Jul 9, 2009, 1:04:25 PM7/9/09
to Lift
Bit of googling and came across some wicket/scala folks having a
similar problem who got around the problem with the following:

implicit def listToJavaList[T](l: Seq[T]) = l.foldLeft(new
java.util.ArrayList[T](l.size)){(al, e) => al.add(e); al}

and magically I can pull out the value now. What I can't do now are
pull out an object's properties for example:


#foreach($user in $headers.users)
hello $user.firstname $user.lastname
#end

I reckon the problem is that Velocity must use bean introspection and
without getters I can't get the values.

--Ewan

Marc Boschma

unread,
Jul 9, 2009, 5:16:03 PM7/9/09
to lif...@googlegroups.com
I think your right - see

http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html
and http://scala.sygneca.com/code/defining-bean-properties

for how to add the annotations to the classes for Velocity to be able
to see the properties...

Marc

Ewan

unread,
Jul 10, 2009, 5:10:55 AM7/10/09
to Lift
Thanks. I found that myself along with scala-javautils in github by
Jorge Ortiz which helps out with the converting from Scala collections
to Java ones.

The @BeanProperty annotation works fine for val and vars but is not
helpful if your passing around mapper instances where the attribs/
props are objects. Of course passing a mapper instance to a 3rd party
lib is probably not a good idea anyway so my solution is to add a toDto
() to my class which creates an immutable data transfer object or
value object from its state which when passed into velocity (with
@BeanProperty annotations) works as expected.

Thanks again.

On Jul 9, 10:16 pm, Marc Boschma <marc+lift...@boschma.cx> wrote:
> I think your right - see
>
> http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html
> andhttp://scala.sygneca.com/code/defining-bean-properties
Reply all
Reply to author
Forward
0 new messages