Paul Makepeace <[EMAIL PROTECTED]> writes:

> I'm converting some PHP to TT and am having trouble with the extra
> whitespace that's left behind after TT does its processing. This extra
> \s+ throws out some HTML renderers like IE.
>
> For example,
>
> <td>
> [% IF 1 %]
>   <img src="foo.jpg">
> [% END %]
> </td>
>
> the best I can get (with POST_CHOMP and FILTER collapse) is,
>
>   <td> <img src="foo.jpg"></td>
>
> That extra space between the td and img can damage the rendering.

Yeah, that's been haunting me often in table-based layouts.

> Is there a way to have TT remove this extra whitespace?

If it isn't feasible to put the whole td content in one line,
I usually move the closing TT tag immediately before the element:

<td>
[%- IF 1
  %]<img src="foo.jpg">
[%- END %]
</td>

> **
>
> I came up with a solution, but would rather something that's already in
> core.
>
>   $template->context->define_filter( scrunch => sub {
>     my $data = $_[0];
>     s/(?<=[^\w\s])\s+//g, s/\s+(?=[^\w\s])//g for $data;
>     $data;
>   });
>
> Is there a way to have TT apply this filter to its whole output?

I'm only writing this after all the other suggestions because I've
tried such an approach and found that it occasionally removes
whitespace where I don't want it to be removed:

<p>
  Always use the
  [%- IF 1 %]
  <a href="http://www.template-toolkit.org/";>Template Toolkit</a>
  [%- END %]
  for template processing.
</p>

You usually want whitespace removed before (most) block-level elements
but only *collapsed* before inline elements like <strong>, <a>, ...
-- 
Cheers,
haj


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

Reply via email to