You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Diane Holt <ho...@yahoo.com> on 2002/03/06 20:40:55 UTC

Re: Getting lines of text individually

>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?

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:

  #foreach ( $items in $section.getChildren() )
and
  #foreach ( $items in $subsection.getChildren() )

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.

Thanks!
Diane

=====
(holtdl@yahoo.com)



__________________________________________________
Do You Yahoo!?
Try FREE Yahoo! Mail - the world's greatest free email!
http://mail.yahoo.com/

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting lines of text individually

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 3/6/02 2:40 PM, "Diane Holt" <ho...@yahoo.com> 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

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
My inner cowboy needs to yodel.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting lines of text individually

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 3/6/02 3:56 PM, "Corneil du Plessis" <co...@mweb.co.za> wrote:

> Create a Toolbox class that will use the StringTokenizer to return the
> lines. From the XML point of view those lines are part of one text element.

Yes, I thought so too - but I have seen some of the API's return multiple
text nodes...


-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Java : the speed of Smalltalk with the simple elegance of C++... 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Getting lines of text individually

Posted by Corneil du Plessis <co...@mweb.co.za>.
Create a Toolbox class that will use the StringTokenizer to return the
lines. From the XML point of view those lines are part of one text element.

Corneil du Plessis
(h) +27 (11) 918-1592
(w) +27 (82) 802-5768

-----Original Message-----
From: Diane Holt [mailto:holtdl@yahoo.com]
Sent: Wednesday, March 06, 2002 9:41 PM
To: velocity-user@jakarta.apache.org
Subject: Re: Getting lines of text individually


>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?

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:

  #foreach ( $items in $section.getChildren() )
and
  #foreach ( $items in $subsection.getChildren() )

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.

Thanks!
Diane

=====
(holtdl@yahoo.com)



__________________________________________________
Do You Yahoo!?
Try FREE Yahoo! Mail - the world's greatest free email!
http://mail.yahoo.com/

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>