velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Burton" <bi...@progress.com>
Subject Re: indentation of templates
Date Mon, 11 Mar 2002 23:59:46 GMT
Hi Sebastien,

Glad to hear you like Velocity.

I have recently run into the same extra whitespace issue while working on
a DVSL stylesheet.  The #if directive never seems to gobble up leading
whitespace even if it's the only thing on the line.  Whereas the #set
directive does do this.

What I did to allow indentation of directives while eliminating the extra
whitespace is to use multiline comments to hide whitespace.  If these
comments are used before #if, #else, #elseif and matching #end's as well
as before invoking other tools or macros, you can get the output to be
indented properly.  Here's an example:

#*       *##if (this-is-true)
           output true stuff
#*         *#$context.applyTemplates("section")
#*       *##else
           output false stuff
#*         *##execute_a_macro()
#*       *##end

Even though the above looks odd, I like it much better than putting
everything flush left.

The only thing is once in a while, I've forgotten the # before the
directive and put *#else instead of *##else.

If you need to eliminate blank lines, start the #* at the end of the prior
line to comment out the enclosed newline.

-Bill

Sebastien Chassande-Barrioz wrote:
> 
> Hi,
> 
> I am new user of the velocity product since few days. I very happy of
> the simplicity to use it and its capacities. I noticed with my small
> experience of velocity (4 full days) that the "code" of my templates is
> not very readable. For example it is impossible to indent a macro
> because the spaces or the tabulations are generated.
> Belove you can find one of my velocimacro which is not really readable
> by another person except the author (...me) without efforts. Indeed this
> macro contains several control structures (#if #foreach).
> I think that one of the aims of the velocity product is to simplify the
> generation and to make reachable (readable) the generator, therefore the
> templates, by other people than the author.
> 
> Do you have advices to make more readable a template ?
> Otherwise, this aspect is it in your todo list ?
> 
> best regards,
> Seb
> --
> Chassande-Barrioz Sebastien
> INRIALPES - SIRAC/SARDES Team - ObjectWeb initiative
> Mail job : sebastien.chassande@inrialpes.fr
> Phone : 04.76.61.55.16
> 
> ----------------------------------------------------------------
> #############################
> ## MACRO writetGenClassDef ##
> #############################
> #macro(writetGenClassDef $gcr $fieldName $level)
> #set($nd = $gcr.getRefNameDef($cparam.getMapperName(),
> $cparam.getMappingName()))
> #set($arraySize = ($nd.getFieldRefNumber() + $nd.getHiddenFieldNumber()))
>                 fds = new FieldDesc[$arraySize];
> #set($gcVarName = "${fieldName}GCM${mappingTools.getGenclass($level)}")
> #set($i=0)
> #foreach($ndField in $nd.iterateField())
> #if ($tools.isFieldName($ndField))
> #set($realField=$class.getTypedElement($ndField.getField()))
> #else
> #set($realField=$ndField)
> #end ##else
> #writeFieldDescFilling($realField "fds[$i]")
> #set($i=($i+1))
> #end ##foreach
>                 ((GenClassProp) $gcVarName ).setIdentifierDesc(fds);
> ##
> ## write the initialization of each field of the index of the genclass
> ##
> #set($gc = $gcr.getGenClass())
>                 fds = new FieldDesc[${mappingTools.getIndexNumber($gc)}];
> #set($i=0)
> #foreach($indexField in $gc.iterateIndexField())
> #writeFieldDescFilling($indexField "fds[$i]")
> #set($i=($i+1))
> #end ##foreach
>                 ((GenClassProp) $gcVarName ).setIndexDesc(fds);
> ##
> ##write element
> ##
> #if($gcr.isPrimitive())
>                 fds = new FieldDesc[1];
> #writeFieldDescFilling($gcr.getPrimitiveElement() "fds[0]")
> #elseif ($gcr.isClassRef() || $gcr.isGenClassRef())
> #set($elemNd=$gcr.getClassRef().getNameDef($cparam.getMapperName(),
> $cparam.getMappingName()))
> #set($i=1)
> #set($size=${elemNd.getHiddenFieldNumber()}+1)
>                 fds = new FieldDesc[$size];
> #writeCreateFieldDescRefTop("fds[0]")
> #foreach($elemField in $elemNd.iterateField())
> #writeFieldDescFilling($elemField "fds[$i]")
> #set($i=($i+1))
> #end
> #end ##test element type
>                 ((GenClassProp) $gcVarName).setElemDesc(fds);
> #writeInitGenClassMapping($gcr $gcVarName $level $fieldName)
> #if($level==0)
> #set($g='"')
> #set($return1= "{$g${gcr.getGenClassName()}$g")
> #else
> #set($return1="$return1, $g${gcr.getGenClassName()}$g")
> #end
> #if($gcr.isGenClassRef())
> #set($level=($level+1))
> #writetGenClassDef(
> $gcr $fieldName $level)
> #else
> #set($return1 = "${return1}}")
> #if($gcr.isClassRef())
> #set($return2=$gcr.getClassRef().getMOClass())
> #elseif ($gcr.isPrimitive())
> #set($return2=$gcr.getPrimitiveElement())
> #end
> #end
> #end
> ##
> ##
>

--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message