So, I have been thinking about it and I'd like to propose two new features
with whitespace chomping. Both of these are low cost to implement, but do
add a bit of user convenience.
The first is to add a new chomp type. Currently CHOMP_ALL or the '-' modifier
doesn't really CHOMP_ALL. It chomps everything on the same line. This has
caused several posts in the short time I've been on the mailing list. I'd
like to see a new chomp type that removes ALL whitespace in a preceding text
chunk. Since CHOMP_ALL is taken I was thinking maybe CHOMP_KILL, or perhaps
CHOMP_ALL_NO_REALLY_I_MEAN_ALL, OR CHOMP_DIE_DIE_DIE (But I would settle for
CHOMP_KILL). Logically the constant would take the value of 3 (or I guess 4
if you want chomp constants to work as a bitset - but I think that is
overkill). To go along with CHOMP_KILL I think that probably the best
modifier would be '~'.
So the table would be:
Constant name Constant Value Tag Modifier
CHOMP_NONE 0 +
CHOMP_ALL 1 -
CHOMP_COLLAPSE 2 = # does this modifier work?
CHOMP_KILL 3 ~
So CHOMP_KILL would do the following fairly desirable thing (no hacks
required).
"
[%~ 1 %]
[%~ 2 ~%]
[% 3 ~%]
"
Which would print 123 without any other whitespace. Note that the leading ~
on the 1 and the trailing ~ on the 3 do the same thing that TRIM => 1 would
do in this case, but would be very different with the following case.
" [%~ ' ' ~%] "
With CHOMP_KILL the final string would be ' ' while with TRIM => 1 the
final string would be ''.
Codewise this is very simple to implement in the split_text method of the
Template/Parser.pm. Sorry - no patch at this time.
OK - Request number two. Can we use the Tag Modifier in place of the CONSTANT
name. Consider:
use Template;
my $t = Template->new(PRE_CHOMP => '-', POST_CHOMP => '~');
As opposed to:
use Template qw(:chomp);
my $t = Template->new(PRE_CHOMP => CHOMP_ALL, POST_CHOMP => CHOMP_KILL);
Or in a bad case:
use Template;
my $t = Template->new(PRE_CHOMP => 1, POST_CHOMP => 3);
Allowing the modifier to be used isn't really saving that much (it isn't that
hard to import the constants) - but then people would be less likely to
hardcode the constants and they wouldn't have to remember the constant names,
and if the CHOMP_KILL proposal is accepted - they wouldn't have to wonder
which function CHOMP_ALL vs CHOMP_KILL does (which one really gets rid of
whitespace vs just the whitespace on the same line).
It also won't be much of a hit to allow the modifier names as the check only
needs to happen once at configuration time.
If there are any questions, let me know. I think these requests are sane
enough for most people and won't cause any hardship for those who don't want
to use them.
Paul Seamons
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates