You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Robert Kuzelj <ro...@yahoo.com> on 2001/06/20 18:42:54 UTC

avoiding linebreaks

hi,

is there a possebillity to avoid linebraks in
velocity 1.1?

what i would do is something like:
>>>>>>>>>>>>>
#for ($elem in $elems)
    $elem
#end
>>>>>>>>>>>>>
which should look like
1,2,3,4,5
given that elems is: [1,2,3,4,5]

ciao robertj


=====
itemj
http://www.itemj.com
Robert Kuzelj          mobil 0177  5302230
Ramonvillestr.6        tel   06039 930223
61184 Karben           fax   06039 2224

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Re: avoiding linebreaks

Posted by Christoph Reck <Ch...@dlr.de>.
I've found a nicer desing-pattern for joining that avoids 
the #if in favour of a #set that does not depend on the 
velocity count:

#macro( join $list $sep )#set( $s_ = "" )
#foreach( $elem_ in $list )$s_$elem_#set( $s_ = $sep )#end
#end

And you use it as:
 
#join($elems, ",")

:) Christoph


Jeremy Leader wrote:
> 
> How about a macro (off the top of my head):
> 
> #macro (join $list $sep)#foreach($elem in $list)#if($velocityCount>1)$sep#end$elem$end#end
> 
> (I'm not certain if you can string it all on one line like that,
> but I think you'd want to, to avoid extraneous white space in
> the result).
> 
> Then you use it as:
> 
> #join($elems, ",")
> 
> Jeremy Leader
> 
> PS is $velocityCount local to each #foreach?  If I called
> the above macro from inside another #foreach, could the outer
> loop still use $velocityCount and not have to worry about the
> inner loop (inside the macro) messing it up?
> 
> At 11:31 AM 6/20/01 , Robert Kuzelj wrote:
> >hi geir,
> > >
> > > Or, with commas
> > >
> > > #foreach($elem in $elems)#if($velocityCount>1),#end$elem#end
> > >
> > > using velocity's default loop counter, $velocityCount, which starts with
> > > 1
> > >
> >ouch!
> >isnt there a more readable way?
> >i am doing a lot of java-generator and python-generator
> >programming. if have to code all those generators
> >that way, its simply not reusable nor changeable.
> >
> >there was a proposol concerning whitespaces in velo.
> >what about that?
> >
> >ciao robertj

Re: avoiding linebreaks

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Robert Kuzelj wrote:
> 
> hi,
> 
> is there a possebillity to avoid linebraks in
> velocity 1.1?
> 
> what i would do is something like:
> >>>>>>>>>>>>>
> #for ($elem in $elems)
>     $elem
> #end
> >>>>>>>>>>>>>
> which should look like
> 1,2,3,4,5
> given that elems is: [1,2,3,4,5]
> 
> ciao robertj
> 

#for( $elem in $elemns)$elem#end

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: avoiding linebreaks

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Jeremy Leader wrote:
> 
> How about a macro (off the top of my head):
> 
> #macro (join $list $sep)#foreach($elem in $list)#if($velocityCount>1)$sep#end$elem$end#end
> 
> (I'm not certain if you can string it all on one line like that,
> but I think you'd want to, to avoid extraneous white space in
> the result).

+1 - yep that will work fine.

> Then you use it as:
> 
> #join($elems, ",")
> 
> Jeremy Leader
> 
> PS is $velocityCount local to each #foreach?  If I called
> the above macro from inside another #foreach, could the outer
> loop still use $velocityCount and not have to worry about the
> inner loop (inside the macro) messing it up?

Yes - it should work correctly.

geir
 

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: avoiding linebreaks

Posted by Jon Stevens <jo...@latchkey.com>.
on 6/20/01 12:41 PM, "Jeremy Leader" <jl...@alumni.caltech.edu> wrote:

> PS is $velocityCount local to each #foreach?  If I called
> the above macro from inside another #foreach, could the outer
> loop still use $velocityCount and not have to worry about the
> inner loop (inside the macro) messing it up?

Yes, it is locally defined correctly.

-jon


Re: avoiding linebreaks

Posted by Jeremy Leader <jl...@alumni.caltech.edu>.
How about a macro (off the top of my head):

#macro (join $list $sep)#foreach($elem in $list)#if($velocityCount>1)$sep#end$elem$end#end

(I'm not certain if you can string it all on one line like that,
but I think you'd want to, to avoid extraneous white space in
the result).

Then you use it as:

#join($elems, ",")

Jeremy Leader

PS is $velocityCount local to each #foreach?  If I called
the above macro from inside another #foreach, could the outer
loop still use $velocityCount and not have to worry about the
inner loop (inside the macro) messing it up?

At 11:31 AM 6/20/01 , Robert Kuzelj wrote:
>hi geir,
> > 
> > Or, with commas
> > 
> > #foreach($elem in $elems)#if($velocityCount>1),#end$elem#end
> > 
> > using velocity's default loop counter, $velocityCount, which starts with
> > 1
> > 
>ouch!
>isnt there a more readable way?
>i am doing a lot of java-generator and python-generator
>programming. if have to code all those generators
>that way, its simply not reusable nor changeable.
>
>there was a proposol concerning whitespaces in velo.
>what about that?
>
>ciao robertj
>
>
>=====
>itemj
>http://www.itemj.com
>Robert Kuzelj          mobil 0177  5302230
>Ramonvillestr.6        tel   06039 930223
>61184 Karben           fax   06039 2224
>
>__________________________________________________
>Do You Yahoo!?
>Get personalized email addresses from Yahoo! Mail
>http://personal.mail.yahoo.com/ 



Re: avoiding linebreaks

Posted by Robert Kuzelj <ro...@yahoo.com>.
hi geir,
> 
> Or, with commas
> 
> #foreach($elem in $elems)#if($velocityCount>1),#end$elem#end
> 
> using velocity's default loop counter, $velocityCount, which starts with
> 1
> 
ouch!
isnt there a more readable way?
i am doing a lot of java-generator and python-generator
programming. if have to code all those generators
that way, its simply not reusable nor changeable.

there was a proposol concerning whitespaces in velo.
what about that?

ciao robertj


=====
itemj
http://www.itemj.com
Robert Kuzelj          mobil 0177  5302230
Ramonvillestr.6        tel   06039 930223
61184 Karben           fax   06039 2224

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Re: avoiding linebreaks

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Robert Kuzelj wrote:
> 
> hi,
> 
> is there a possebillity to avoid linebraks in
> velocity 1.1?
> 
> what i would do is something like:
> >>>>>>>>>>>>>
> #for ($elem in $elems)
>     $elem
> #end
> >>>>>>>>>>>>>
> which should look like
> 1,2,3,4,5
> given that elems is: [1,2,3,4,5]
> 
> ciao robertj

Or, with commas

#foreach($elem in $elems)#if($velocityCount>1),#end$elem#end

using velocity's default loop counter, $velocityCount, which starts with
1

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: avoiding linebreaks

Posted by Edward Muller <em...@learningpatterns.com>.
New to Velocity myself...

But I think that would be great (modifying the whitespace rules)!

> The simplest way to avoid linebreaks in
> velocity is to escape them with a #* comment *#.
> 
> #for ($elem in $elems)#*
> *#$elem#*
> *##end
> 
> I have proposed previously to enhance the whitespace
> handling to make any standalone directive (with 
> whitespaces around it and nothing else) to not emit 
> anything to the output. Currently #end and #set
> directives gobble up only trailing whitespaces 
> including the EOL.
> 
> This would change the behaviour of current templates,
> but would be much more consistent and desireable.
> 
> :) Christoph
> 
> 
> Robert Kuzelj wrote:
>> 
>> hi,
>> 
>> is there a possebillity to avoid linebraks in
>> velocity 1.1?
>> 
>> what i would do is something like:
>> >>>>>>>>>>>>>
>> #for ($elem in $elems)
>>     $elem
>> #end
>> >>>>>>>>>>>>>
>> which should look like
>> 1,2,3,4,5
>> given that elems is: [1,2,3,4,5]
>> 
>> ciao robertj
>> 
>> =====
>> itemj
>> http://www.itemj.com
>> Robert Kuzelj          mobil 0177  5302230
>> Ramonvillestr.6        tel   06039 930223
>> 61184 Karben           fax   06039 2224
>> 
>> __________________________________________________
>> Do You Yahoo!?
>> Get personalized email addresses from Yahoo! Mail
>> http://personal.mail.yahoo.com/


-- 
Edward Muller
Director of Information Services
LearningPatterns.com Inc.

Direct: 212-486-9064 x 115
Fax: 212-202-3822
Email: emuller@learningpatterns.com

http://www.learningpatterns.com


Re: avoiding linebreaks

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Joaquim Ramos de Carvalho wrote:
> 
> My company created a small template language for bibliographic
> applications where this problem was very important. The way we handled
> it was by two special commands (the syntax is different, I am putting it
> as they would look in velocity)
> 

[SNIP]

> This is not a feature request, just a note on how the problem was
> handled in a similar situation. Note,however that an addition of this
> type would be backwards compatible with existing templates.
> 
> In fact in the usage we make of Velocity linebreaks are not a problem
> because we keep logic to a minimum in templates and push things to
> context objects. In our in house template language the templates had
> heavy logic and thus could produce dozens of blank lines -- hence the
> output/echo solutionm which is in fact was a sympton of poor
> Logic/presentation separation.
> 

:)

I was reading this thinking "Hm, interesting, but it seems like a
band-aid for having so much business/application logic in the
templates...."

Have you switched to Velocity?

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: avoiding linebreaks

Posted by Joaquim Ramos de Carvalho <jr...@bookmarc.pt>.
My company created a small template language for bibliographic 
applications where this problem was very important. The way we handled 
it was by two special commands (the syntax is different, I am putting it 
as they would look in velocity)

1)
#output(arg)    where arg is "on", "off", "hold" or "restore".
     #ouput(off) would cancel any output from the template. Purely 
computational sequences would be "executed" without any output.
For example:

    #output(off)
   ## holding off ouput to avoid blank lines from inside the loop
    #for ($elem in $elements)
     ## this is just a method call, no interesting output
      $elem.translate($userLanguage)
    #end
   #output (on)

This would produce a single line break after the line (#output(on))

#output(hold) and #output(restore) function like a push and pop of the 
current "output" state. This allows macros or inserted templates to save 
the current output state (from the calling template), change it if they 
need to and them restoring it back to the original state.

The other command is #echo which overrides the current output state for 
a single statement.

For example:
#output(off)
#for ($elem in $elems)
    #echo( $elem)
#end
#output(on)

would produce the value of each $elem and a single linebreak after 
#output(on)

#echo makes it easier to handle situations where you want to avoid most 
of the output and it would be awkward to be using #output(on) 
#output(off) .

This is not a feature request, just a note on how the problem was 
handled in a similar situation. Note,however that an addition of this 
type would be backwards compatible with existing templates.

In fact in the usage we make of Velocity linebreaks are not a problem 
because we keep logic to a minimum in templates and push things to 
context objects. In our in house template language the templates had 
heavy logic and thus could produce dozens of blank lines -- hence the 
output/echo solutionm which is in fact was a sympton of poor 
Logic/presentation separation.

Joaquim



On Thursday, June 21, 2001, at 08:48 AM, Christoph Reck wrote:

> The simplest way to avoid linebreaks in
> velocity is to escape them with a #* comment *#.
>
> #for ($elem in $elems)#*
> *#$elem#*
> *##end
>
> I have proposed previously to enhance the whitespace
> handling to make any standalone directive (with
> whitespaces around it and nothing else) to not emit
> anything to the output. Currently #end and #set
> directives gobble up only trailing whitespaces
> including the EOL.
>
> This would change the behaviour of current templates,
> but would be much more consistent and desireable.
>
> :) Christoph
>
>
> Robert Kuzelj wrote:
>>
>> hi,
>>
>> is there a possebillity to avoid linebraks in
>> velocity 1.1?
>>
>> what i would do is something like:
>>>>>>>>>>>>>>>
>> #for ($elem in $elems)
>>     $elem
>> #end
>>>>>>>>>>>>>>>
>> which should look like
>> 1,2,3,4,5
>> given that elems is: [1,2,3,4,5]
>>
>> ciao robertj
>>
>> =====
>> itemj
>> http://www.itemj.com
>> Robert Kuzelj          mobil 0177  5302230
>> Ramonvillestr.6        tel   06039 930223
>> 61184 Karben           fax   06039 2224
>>
>> __________________________________________________
>> Do You Yahoo!?
>> Get personalized email addresses from Yahoo! Mail
>> http://personal.mail.yahoo.com/
>

Re: avoiding linebreaks

Posted by Christoph Reck <Ch...@dlr.de>.
The simplest way to avoid linebreaks in
velocity is to escape them with a #* comment *#.

#for ($elem in $elems)#*
*#$elem#*
*##end

I have proposed previously to enhance the whitespace
handling to make any standalone directive (with 
whitespaces around it and nothing else) to not emit 
anything to the output. Currently #end and #set
directives gobble up only trailing whitespaces 
including the EOL.

This would change the behaviour of current templates,
but would be much more consistent and desireable.

:) Christoph


Robert Kuzelj wrote:
> 
> hi,
> 
> is there a possebillity to avoid linebraks in
> velocity 1.1?
> 
> what i would do is something like:
> >>>>>>>>>>>>>
> #for ($elem in $elems)
>     $elem
> #end
> >>>>>>>>>>>>>
> which should look like
> 1,2,3,4,5
> given that elems is: [1,2,3,4,5]
> 
> ciao robertj
> 
> =====
> itemj
> http://www.itemj.com
> Robert Kuzelj          mobil 0177  5302230
> Ramonvillestr.6        tel   06039 930223
> 61184 Karben           fax   06039 2224
> 
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/

RE: avoiding linebreaks

Posted by David Rees <dr...@ebetinc.com>.
> is there a possebillity to avoid linebraks in
> velocity 1.1?
>
> what i would do is something like:
> >>>>>>>>>>>>>
> #for ($elem in $elems)
>     $elem
> #end
> >>>>>>>>>>>>>
> which should look like
> 1,2,3,4,5
> given that elems is: [1,2,3,4,5]

Best thing to do then is this:

#for ($elem in $elems)$elem#end

Not as readable, but does the trick.

There was some discussion about this previously in the archives, you may
want to check them.

-Dave