|
List Info
Thread: Removing even more whitespace
|
|
| Removing even more whitespace |

|
2006-01-29 14:53:15 |
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.
Is there a way to have TT remove this extra whitespace?
**
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/(?<=[^ws])s+//g, s/s+(?=[^ws])//g for $data;
$data;
});
Is there a way to have TT apply this filter to its whole
output?
Paul
--
Paul Makepeace .............................. http://paulm.com/inchoate/
"If you are not you, then you've got a disco on your
hands."
-- http://paulm.com/to
ys/surrealism/
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-29 17:12:45 |
>>>>> "Paul" == Paul Makepeace
<tt2.org paulm.com> writes:
Paul> I'm converting some PHP to TT and am having trouble
with the extra
Paul> whitespace that's left behind after TT does its
processing. This extra
Paul> s+ throws out some HTML renderers like IE.
Paul> For example,
Paul> <td>
Paul> [% IF 1 %]
Paul> <img src="foo.jpg">
Paul> [% END %]
Paul> </td>
Paul> the best I can get (with POST_CHOMP and FILTER
collapse) is,
Paul> <td> <img
src="foo.jpg"></td>
Paul> That extra space between the td and img can damage
the rendering.
Paul> Is there a way to have TT remove this extra
whitespace?
Setting POST_CHOMP to 1 is my favorite, because it *mostly*
does the right
thing. But it's not psychic, since you put a newline after
your initial text.
Either also set PRE_CHOMP to 1 (which would swallow that,
but sometimes it'll
swallow too much), or leave POST_CHOMP at 1 and write that
as:
<td>
[%- IF 1 %]
<img src="foo.jpg">
[%- END %]
</td>
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-29 18:36:09 |
On Sun, 29 Jan 2006, Paul Makepeace wrote:
> <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.
Isn't that extra space in there explicitly ? Shouldn't you
write
<td>
[% IF 1 %]
<img src="foo.jpg">
[% END %]
</td>
i.e. with no indent of the img tag ?
Simon.
--
"They tend to come out a colour called 'Pants left in
wash' "
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-29 23:24:37 |
Je 2006-01-29 17:12:45 +0000, Randal L. Schwartz skribis:
> >>>>> "Paul" == Paul Makepeace
<tt2.org paulm.com> writes:
>
> Paul> I'm converting some PHP to TT and am having
trouble with the extra
> Paul> whitespace that's left behind after TT does
its processing. This extra
> Paul> s+ throws out some HTML renderers like IE.
>
> Paul> For example,
>
> Paul> <td>
> Paul> [% IF 1 %]
> Paul> <img src="foo.jpg">
> Paul> [% END %]
> Paul> </td>
>
> Paul> the best I can get (with POST_CHOMP and FILTER
collapse) is,
>
> Paul> <td> <img
src="foo.jpg"></td>
>
> Paul> That extra space between the td and img can
damage the rendering.
>
> Paul> Is there a way to have TT remove this extra
whitespace?
>
> Setting POST_CHOMP to 1 is my favorite, because it
*mostly* does the right
> thing. But it's not psychic, since you put a newline
after your initial text.
> Either also set PRE_CHOMP to 1 (which would swallow
that, but sometimes it'll
> swallow too much), or leave POST_CHOMP at 1 and write
that as:
>
> <td>
> [%- IF 1 %]
> <img src="foo.jpg">
> [%- END %]
> </td>
Hmm, did you actually try this? It didn't work for me, nor
is it AIUI
documented to work (although frankly some of the _CHOMP docs
were
confusing to some IRC wonks & I who were futzing with
this). The TT
engine doesn't seem to mess with stuff outside of its tags.
Paul
--
Paul Makepeace .............................. http://paulm.com/inchoate/
"What is a brown derby? Where the heart is."
-- http://paulm.com/to
ys/surrealism/
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-29 23:28:33 |
Je 2006-01-29 18:36:09 +0000, Simon Wilcox skribis:
> On Sun, 29 Jan 2006, Paul Makepeace wrote:
>
> > <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.
>
> Isn't that extra space in there explicitly ? Shouldn't
you write
>
> <td>
> [% IF 1 %]
> <img src="foo.jpg">
> [% END %]
> </td>
>
> i.e. with no indent of the img tag ?
Yeah, that's one way but I hardly ever see unindented HTML
or unindented
template code (and wouldn't want to maintain that anyway!).
Besides it
would also require a degree of rewriting of the code and
partly this
exercise is to demonstrate how easy it is to convert PHP to
Template ;)
(Which in fairness, besides this bit, it has been.)
P
--
Paul Makepeace .............................. http://paulm.com/inchoate/
"What is that sun doing in that sky? The
unforgiven."
-- http://paulm.com/to
ys/surrealism/
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-29 23:56:21 |
On 1/29/06, Paul Makepeace <tt2.org paulm.com> wrote:
> Je 2006-01-29 17:12:45 +0000, Randal L. Schwartz
skribis:
> > <td>
> > [%- IF 1 %]
> > <img src="foo.jpg">
> > [%- END %]
> > </td>
>
> Hmm, did you actually try this? It didn't work for me,
nor is it AIUI
> documented to work (although frankly some of the _CHOMP
docs were
> confusing to some IRC wonks & I who were futzing
with this). The TT
> engine doesn't seem to mess with stuff outside of its
tags.
The problem is that you added extra spaces in front of your
img tag.
The chomp support in TT only makes changes up to the first
newline it
encounters (whether it is moving forwards (post chomp) or
backwards
(pre chomp) in a string)
If you are post chomping, then TT will remove all spaces up
to and
including the first newline (unless a non-whitespace
character is
found before the newline). If you are prechomping it works
exactly
the same way except working backwards into the string. All
whitespace
up to and including the first newline is chomped (unless a
non-whitespace character is found before the newline).
Since you are post chomping, and there are spaces 'after'
the newline,
they are never altered.
use Template;
my $template = Template->new;
$template->process(*DATA) or die $template->error;
__DATA__
SPACES REMAIN: <td>
[%- IF 1 -%]
<img src="foo.jpg">
[%- END -%]
</td>
NO EXTRA SPACES: <td>
[%- IF 1 %]
<img src="foo.jpg">
[%- END %]
</td>
And here is the output:
SPACES REMAIN: <td> <img
src="foo.jpg"></td>
NO EXTRA SPACES: <td><img
src="foo.jpg"></td>
Cheers,
Cees
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-29 23:57:51 |
>>>>> "Paul" == Paul Makepeace
<tt2.org paulm.com> writes:
>> <td>
>> [%- IF 1 %]
>> <img src="foo.jpg">
>> [%- END %]
>> </td>
Paul> Hmm, did you actually try this?
No, sorry. :(
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-30 00:04:28 |
Paul Makepeace wrote:
> 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/(?<=[^ws])s+//g, s/s+(?=[^ws])//g for
$data;
> $data;
> });
>
> Is there a way to have TT apply this filter to its
whole output?
You could put the TT output into a variable like so:
my $process_result = $template->process($filename,
%vars, $result);
Then just do your regular expression:
$result =~ foo;
The ideal solution would probably be a patch to handle this.
It looks like there are CHOMP_ALL, CHOMP_COLLAPSE, and
CHOMP_NONE constants for the POST_CHOMP and PRE_CHOMP config
options, so it might be good to just add another one,
"CHOMP_ALL_EVEN_AFTER_NEWLINE". (I'm sure there'd
be a better name.) Or maybe a new config option
"CHOMPS_GO_OVER_NEWLINES" might be better.
http://www.template-toolkit.org/docs/plain/Manual/C
onfig.html references the current CHOMP constants. (None
of which do what you want, since they only remove whitespace
up to a newline rather than all whitespace.)
-- Josh
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-30 00:15:03 |
On 1/29/06, Paul Makepeace <tt2.org paulm.com> wrote:
> Je 2006-01-29 18:36:09 +0000, Simon Wilcox skribis:
> > Isn't that extra space in there explicitly ?
Shouldn't you write
> >
> > <td>
> > [% IF 1 %]
> > <img src="foo.jpg">
> > [% END %]
> > </td>
> >
> > i.e. with no indent of the img tag ?
>
> Yeah, that's one way but I hardly ever see unindented
HTML or unindented
> template code (and wouldn't want to maintain that
anyway!). Besides it
> would also require a degree of rewriting of the code
and partly this
> exercise is to demonstrate how easy it is to convert
PHP to Template ;)
Just curious, but how does PHP handle this? I thought that
it never
touched anything outside the <? ?> tags (I haven't
used it since the
90s, so my knowledge of it is very outdated).
If you wrote this HTML page without any dynamic elements,
you would be
forced to write it like this:
<td><img src="foo.jpg"></td>
So why not do the same with the dynamic code:
<td>[% IF 1 %]<img src="foo.jpg">[%
END %]</td>
I know that can get kind of busy if you have a long IF
condition, but
you can always break the code inside the [% %] up over
multipl
lines...
Cheers,
Cees
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
| Removing even more whitespace |

|
2006-01-30 08:27:13 |
Paul Makepeace wrote:
> Is there a way to have TT remove this extra whitespace?
Nope, not at present. It only chomps up to the newline and
no further.
> I came up with a solution, but would rather something
that's already in
> core.
[...]
> Is there a way to have TT apply this filter to its
whole output?
Yep, set the WRAPPER option to point to a template like
this:
[% content FILTER scrunch %]
HTH
A
_______________________________________________
templates mailing list
templates template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/t
emplates
|
|
|
|