S.session.get.processSurroundAndInclude has a couple issues, first is
that it seems to be a much larger stick than you need, and the other
reason is that you are using "get" (which is for Option, I presume you
mean open_!, since S.session is a box) which is not safe -- it will
throw NPE in the case where a session is not available.
I think the more idiomatic way is probably more like
def forEachMsg(in: NodeSeq): NodeSeq = {
val msgs = somehowGetAListOfModelObjects()
msgs.flatMap { msg =>
bind("msg", in, "field1" -> msg.field1, "field2" ->
msg.field2);
}
}
And then inline at the call side:
<lift:blabla.forEachMsg>
<msg:field1 />
<msg:field2 />
</lift:blabla.forEachMsg>
Is there something peculiar about your use case where you really want
to do lift:embed and so on?
-Ross
On Dec 23, 2009, at 1:26 PM, Erkki Lindpere wrote:
> This has probably come up previously, but I couldn't find anything
> even though I think a couple of days ago I read about something
> similar:
>
> I want to do a snippet that behaves similarly to JSTL-s forEach tag.
> Not exactly the same, but the idea is that it gets some list of model
> objects, then goes through it element by element and processes the
> input NodeSeq for each element. What I came up with is this:
>
> def foreach(in: NodeSeq) = {
> val msgs = somehowGetAListOfModelObjects();
> msgs.flatMap { msg =>
> // set the current object (CurrentMessage is a ThreadLocal)
> CurrentMessage.set(msg)
> // process the contents, with the current object in scope
> S.session.get.processSurroundAndInclude("", in)
> }
> }
>
> And it's called like this:
> <lift:blabla.foreach>
> <lift:embed what="message.html" />
> </lift:blabla.foreach>
>
>
> Is there a more correct solution to this?
>
> --
>
> You received this message because you are subscribed to the Google
> Groups "Lift" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]
> .
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en
> .
>
>
--
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en.