srins has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
how to chomp extra \n character or white space i am getting in text area using template toolkit.
I have an arrays of hashes,
my @add=( { name=>"Submode", value=>[ 'Ford Prefect', 'Slartibartfast' ], } ); i pass this array of to template by my %data = ( add => \@add ); my $template = Template->new({ }); $template->process(temp.tmpl, \%data) || die $template->error();

In the template file i process as
[% FOREACH element = add %] <textarea rows="4" name="[%element.name %]" > [% FOREACH option = element.value %] [% option %] [%END%] </textarea> [%END%]

when it is displayed in the template in text area
$
Ford Prefect$
$
Ford Prefect$
$
$
These extra characters are coming ie displaying with '\n'. Displaying it as

Ford Prefect

Ford Prefect
i tried with
"% option.chomp % and also % option.trim %"

but as the result nothing is displaying in the text area.
how to modify my code to trim or chomp extra characters or '\n' characters using template toolkit.
  • Comment on how to chomp or trim extra characters like \n ,white spaces in text area using template toolkit
  • Select or Download Code

Replies are listed 'Best First'.
Re: how to chomp or trim extra characters like \n ,white spaces in text area using template toolkit
by Fang (Pilgrim) on Nov 24, 2005 at 09:15 UTC

    You can either use pre/post-chomping in your directives, as in:

    [% FOREACH option = element.value %] [%- option -%] [% END %] # this is equivalent to writing those three directives on a single lin +e: # [% FOREACH option = element.value %][% option %][% END %]

    or you can use global options when you create your new Template object if you want such a behaviour applied everywhere:

    my $template = Template->new( { PRE_CHOMP => 1, POST_CHOMP => 1, } );

    You can read more about those options in the Template::Manual::Config doc.

Re: how to chomp or trim extra characters like \n ,white spaces in text area using template toolkit
by holli (Abbot) on Nov 24, 2005 at 17:02 UTC
    ++ for mentioning Slartibartfast in a PM post ;-)

    Also, I must admit that I had to become 30 years old before I realized it's Ford Prefect and not Ford Perfect :-/

    Thanks for that.


    holli, /regexed monk/
      That is because Henry never made a Perfect Ford
Re: how to chomp or trim extra characters like \n ,white spaces in text area using template toolkit
by EvanCarroll (Chaplain) on Nov 24, 2005 at 09:16 UTC
    my @add=( { name=>"Submode", value=>[ 'Ford Prefect', 'Slartibartfast' ], } );

    Add:
    foreach $element (@add) { foreach ( @{$element->{'value'}} ) { chomp; } }


    Update:
    I think first node is more on target, I never use TT =[


    Evan Carroll
    www.EvanCarroll.com