On 07/02/11 14:22, Jiří Pavlovský wrote:
>    Hi,
>
> I Have a block like below. It will produce the following output when
> included with the 'collapse' filter.:
>
> "- name1 , name2 , and name 3 en".
>

It's worth noting that the original problem was caused by a combination 
of white space and newlines, and that there are a few different chomp 
directives that can be used in these situations.  Contiguous white space 
is usually rendered as a single space by HTML, but sometimes, as in the 
case of the space before the comma, all white space needs to be removed.

The following examples assume that the PRE_CHOMP and POST_CHOMP options 
are turned off.

In this simplified example of the problem, there are a number of spaces 
and newlines that end up appearing in the final HTML.  The numbers in 
asterisks denote the newlines:

<p>*1*
[% FOREACH author IN
     ['Larry Wall', 'Randal L. Schwartz', 'brian d foy', 'Tom Phoenix']
%]*2*
     [% author %]*3*
     [% UNLESS loop.last %]*4*
     ,*5*
     [% END %]*6*
[% END %]*7*
</p>

This results in the following HTML:

<p>*1*
*2*
     Larry Wall*3*
     *4*
     ,*5*
     *6*
*2*
     Randal L. Schwartz*3*
     *4*
     ,*5*
     *6*
*2*
     brian d foy*3*
     *4*
     ,*5*
     *6*
*2*
     Tom Phoenix*3*
     *6*
*7*
</p>

When rendered, the above HTML looks like this:

*1* *2* Larry Wall*3* *4* ,*5* *6* *2* Randal L. Schwartz*3* *4* ,*5* 
*6* *2* brian d foy*3* *4* ,*5* *6* *2* Tom Phoenix*3* *6* *7*

The CHOMP_ONE options, which can be applied to an individual TT 
directive by adding a hyphen (-), will remove any white space and a 
single newline either before or after the directive:

Remove white space and a single newline before the directive:

[%- author %]

Remove white space and a single newline after the directive:

[% author -]

However, even liberal application of the CHOMP_ONE option doesn't 
produce the right effect in the above example, where there is white 
space and two newlines between the author and the comma:

<p>
[%- FOREACH author IN
     ['Larry Wall', 'Randal L. Schwartz', 'brian d foy', 'Tom Phoenix']
-%]
     [%- author -%]
     [%- UNLESS loop.last -%]
     ,
     [%- END -%]
[%- END -%]
</p>

<p>Larry Wall    ,Randal L. Schwartz    ,brian d foy    ,Tom Phoenix</p>

When rendered, the above HTML looks like this:

Larry Wall ,Randal L. Schwartz ,brian d foy ,Tom Phoenix

To resolve this, the CHOMP_COLLAPSE (=) and CHOMP_GREEDY (~) options can 
be used.  CHOMP_COLLAPSE changes all white space and newlines to a 
single space.  CHOMP_GREEDY removes all whitespace and newlines:

<p>
[%- FOREACH author IN
     ['Larry Wall', 'Randal L. Schwartz', 'brian d foy', 'Tom Phoenix']
~%]
     [% author %]
     [%~ UNLESS loop.last ~%]
     ,
     [%= END %]
[%~ END -%]
</p>

<p>Larry Wall, Randal L. Schwartz, brian d foy, Tom Phoenix</p>


_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to