velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <>
Subject Re: Getting lines of text individually
Date Wed, 06 Mar 2002 20:35:41 GMT
On 3/6/02 2:40 PM, "Diane Holt" <> wrote:

>> Sure
>> #set($lines= ["Hello","there","Diane"])
>> #foreach($line in $lines)
>> $line
>> #end
>> Will output
>> Hello
>> there
>> Diane
> Oops -- guess I should have been more clear. What I meant to ask was, is
> there any way to have individual lines of input text be dealt with? In
> other words, if I have:
> <sometag>
> Three lines
> of text in
> an XML file.
> </sometag>
> and I don't want to have to code a hard linebreak on them, but rather, use
> a #foreach to do that for me, is there a way to do that?

Ah, this is more subtle :)

> Note: I don't know Velocity or Anakia, so I may be asking the question all
> wrong, but in looking at the site.vsl file (for the Ant pages), it has a
> couple of #foreach loops:

Don't forget DVSL, as that can help too, or maybe pieces and tools from
there can help.

The problem you might run into is how you 'grab' the lines of text, as that
could be considered a single complete text node in your tree, depending upon
the object model (and bugs in the implementation...).  Generally I have seen
them come as separate text nodes, but I am not sure what the 'correct' way
would be.
> #foreach ( $items in $section.getChildren() )
> and
> #foreach ( $items in $subsection.getChildren() )

So, yes, you could do something like that - use JDOM or dom4j (my fave at
the moment, but don't tell Jason...), and then get the children of the
<sometag> element ( or get it via Xpath in dom4j, or use Jaxen + JDOM).

> that pass $items to the templates.vm file (if that's the right way to put
> it), and $items seems to be the entire block of text between the (defined)
> <tag></tag>. I'm wondering if there's some way to have it retrieve the
> text and deal with it as individual lines first, have it add a <br></br>
> to the end of each one, then pass the whole block off.

Yes - so parse it in a way that returns N objects, one for each line - then
your approach to the template will work.

I'll try to post some code later...


Geir Magnusson Jr.                           
System and Software Consulting
My inner cowboy needs to yodel.

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message