Changes

Jump to navigation Jump to search
27 bytes added ,  00:03, 22 December 2013
Line 10: Line 10:  
== SYNOPSIS ==
 
== SYNOPSIS ==
 
         use Text::Template;
 
         use Text::Template;
 
+
 
         $template = Text::Template->new(TYPE => ’FILE’,  SOURCE => ’filename.tmpl’);
 
         $template = Text::Template->new(TYPE => ’FILE’,  SOURCE => ’filename.tmpl’);
 
         $template = Text::Template->new(TYPE => ’ARRAY’, SOURCE => [ ... ] );
 
         $template = Text::Template->new(TYPE => ’ARRAY’, SOURCE => [ ... ] );
Line 16: Line 16:  
         $template = Text::Template->new(TYPE => ’STRING’, SOURCE => ’...’ );
 
         $template = Text::Template->new(TYPE => ’STRING’, SOURCE => ’...’ );
 
         $template = Text::Template->new(PREPEND => q{use strict;}, ...);
 
         $template = Text::Template->new(PREPEND => q{use strict;}, ...);
 
+
 
         # Use a different template file syntax:
 
         # Use a different template file syntax:
 
         $template = Text::Template->new(DELIMITERS => [$open, $close], ...);
 
         $template = Text::Template->new(DELIMITERS => [$open, $close], ...);
 
+
 
         $recipient = ’King’;
 
         $recipient = ’King’;
 
         $text = $template->fill_in();  # Replaces ‘{$recipient}’ with ‘King’
 
         $text = $template->fill_in();  # Replaces ‘{$recipient}’ with ‘King’
 
         print $text;
 
         print $text;
 
+
 
         $T::recipient = ’Josh’;
 
         $T::recipient = ’Josh’;
 
         $text = $template->fill_in(PACKAGE => T);
 
         $text = $template->fill_in(PACKAGE => T);
 
+
 
         # Pass many variables explicitly
 
         # Pass many variables explicitly
 
         $hash = { recipient => ’Abed-Nego’,
 
         $hash = { recipient => ’Abed-Nego’,
Line 37: Line 37:  
         # @friends is ( ’me’, ’you’ ),
 
         # @friends is ( ’me’, ’you’ ),
 
         # %enemies is ( loathsome => ..., fearsome => ... )
 
         # %enemies is ( loathsome => ..., fearsome => ... )
 
+
 
         # Call &callback in case of programming errors in template
 
         # Call &callback in case of programming errors in template
 
         $text = $template->fill_in(BROKEN => \&callback, BROKEN_ARG => $ref, ...);
 
         $text = $template->fill_in(BROKEN => \&callback, BROKEN_ARG => $ref, ...);
 
+
 
         # Evaluate program fragments in Safe compartment with restricted permissions
 
         # Evaluate program fragments in Safe compartment with restricted permissions
 
         $text = $template->fill_in(SAFE => $compartment, ...);
 
         $text = $template->fill_in(SAFE => $compartment, ...);
 
+
 
         # Print result text instead of returning it
 
         # Print result text instead of returning it
 
         $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...);
 
         $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...);
 
+
 
Parse template with different template file syntax:
 
Parse template with different template file syntax:
 
         $text = $template->fill_in(DELIMITERS => [$open, $close], ...);
 
         $text = $template->fill_in(DELIMITERS => [$open, $close], ...);
 
         # Note that this is *faster* than using the default delimiters
 
         # Note that this is *faster* than using the default delimiters
 
+
 
         # Prepend specified perl code to each fragment before evaluating:
 
         # Prepend specified perl code to each fragment before evaluating:
 
         $text = $template->fill_in(PREPEND => q{use strict ’vars’;}, ...);
 
         $text = $template->fill_in(PREPEND => q{use strict ’vars’;}, ...);
 
+
 
         use Text::Template ’fill_in_string’;
 
         use Text::Template ’fill_in_string’;
 
         $text = fill_in_string( <<EOM, PACKAGE => ’T’, ...);
 
         $text = fill_in_string( <<EOM, PACKAGE => ’T’, ...);
Line 60: Line 60:  
               Love,
 
               Love,
 
                 G.V.
 
                 G.V.
EOM
+
    EOM
 
+
 
         use Text::Template ’fill_in_file’;
 
         use Text::Template ’fill_in_file’;
 
         $text = fill_in_file($filename, ...);
 
         $text = fill_in_file($filename, ...);
 
+
 
         # All templates will always have ‘use strict vars’ attached to all fragments
 
         # All templates will always have ‘use strict vars’ attached to all fragments
 
         Text::Template->always_prepend(q{use strict ’vars’;});
 
         Text::Template->always_prepend(q{use strict ’vars’;});
Line 78: Line 78:     
               Dear {$title} {$lastname},
 
               Dear {$title} {$lastname},
 
+
 
               It has come to our attention that you are delinquent in your
 
               It has come to our attention that you are delinquent in your
 
               {$monthname[$last_paid_month]} payment.  Please remit
 
               {$monthname[$last_paid_month]} payment.  Please remit
 
               ${sprintf("%.2f", $amount)} immediately, or your patellae may
 
               ${sprintf("%.2f", $amount)} immediately, or your patellae may
 
               be needlessly endangered.
 
               be needlessly endangered.
 
+
 
                               Love,
 
                               Love,
 
+
 
                               Mark "Vizopteryx" Dominus
 
                               Mark "Vizopteryx" Dominus
   Line 91: Line 91:     
               Dear Mr. Gates,
 
               Dear Mr. Gates,
 
+
 
               It has come to our attention that you are delinquent in your
 
               It has come to our attention that you are delinquent in your
 
               February payment.  Please remit
 
               February payment.  Please remit
 
               $392.12 immediately, or your patellae may
 
               $392.12 immediately, or your patellae may
 
               be needlessly endangered.
 
               be needlessly endangered.
 
+
 
                               Love,
 
                               Love,
 
+
 
                               Mark "Vizopteryx" Dominus
 
                               Mark "Vizopteryx" Dominus
   Line 104: Line 104:     
               use Text::Template;
 
               use Text::Template;
 
+
 
               my $template = Text::Template->new(SOURCE => ’formletter.tmpl’)
 
               my $template = Text::Template->new(SOURCE => ’formletter.tmpl’)
 
                 or die "Couldn’t construct template: $Text::Template::ERROR";
 
                 or die "Couldn’t construct template: $Text::Template::ERROR";
 
+
 
               my @monthname = qw(January February March April May June
 
               my @monthname = qw(January February March April May June
 
                                   July August September October November December);
 
                                   July August September October November December);
Line 117: Line 117:  
                           monthname => \@monthname,
 
                           monthname => \@monthname,
 
                           );
 
                           );
 
+
 
               my $result = $template->fill_in(HASH => \%vars);
 
               my $result = $template->fill_in(HASH => \%vars);
 
+
 
               if (defined $result) { print $result }
 
               if (defined $result) { print $result }
 
               else { die "Couldn’t fill in template: $Text::Template::ERROR" }
 
               else { die "Couldn’t fill in template: $Text::Template::ERROR" }
Line 125: Line 125:  
=== Philosophy ===
 
=== Philosophy ===
   −
When people make a template module like this one, they almost always start by inventing a special syntax for substitutions.  For example, they build it so that a string like "%%VAR%%" is replaced with the value of $VAR.  Then they realize the need extra formatting, so they put in some special syntax for formatting.  Then they need a loop, so they invent a loop syntax.  Pretty soon they have a new little template language.
+
When people make a template module like this one, they almost always start by inventing a special syntax for substitutions.  For example, they build it so that a string like "%%VAR%%" is replaced with the value of $VAR.  Then they realize the need extra formatting, so they put in some special syntax for formatting.  Then they need a loop, so they invent a loop syntax.  Pretty soon they have a new little template language.<br />
This approach has two problems: First, their little language is crippled. If you need to do something the author hasn’t thought of, you lose.  Second: Who wants to learn another language?  You already know Perl, so why not use it?
+
 
 +
This approach has two problems: First, their little language is crippled. If you need to do something the author hasn’t thought of, you lose.  Second: Who wants to learn another language?  You already know Perl, so why not use it?<br />
 +
 
    
"Text::Template" templates are programmed in Perl.  You embed Perl code in your template, with "{" at the beginning and "}" at the end.  If you want a variable interpolated, you write it the way you would in Perl. If you need to make a loop, you can use any of the Perl loop constructions.  All the Perl built-in functions are available.
 
"Text::Template" templates are programmed in Perl.  You embed Perl code in your template, with "{" at the beginning and "}" at the end.  If you want a variable interpolated, you write it the way you would in Perl. If you need to make a loop, you can use any of the Perl loop constructions.  All the Perl built-in functions are available.
Line 169: Line 171:  
               The Lord High Chamberlain has gotten 42
 
               The Lord High Chamberlain has gotten 42
 
               things for me this year.
 
               things for me this year.
 
+
 
               That is 25 more than he gave me last year.
 
               That is 25 more than he gave me last year.
   Line 220: Line 222:  
               new Text::Template ( TYPE => ’FILE’, SOURCE => $filename );
 
               new Text::Template ( TYPE => ’FILE’, SOURCE => $filename );
   −
This reads the template from the specified file.  The filename is opened with the Perl "open" command, so it can be a pipe or anything else that makes sense with "open". The "TYPE" can also be "STRING", in which case the "SOURCE" should be a
+
This reads the template from the specified file.  The filename is opened with the Perl "open" command, so it can be a pipe or anything else that makes sense with "open". The "TYPE" can also be "STRING", in which case the "SOURCE" should be a string:
      string:
      
               new Text::Template ( TYPE => ’STRING’,
 
               new Text::Template ( TYPE => ’STRING’,
Line 304: Line 305:     
                   Your Royal Highness,
 
                   Your Royal Highness,
 
+
 
                   Enclosed please find a list of things I have gotten
 
                   Enclosed please find a list of things I have gotten
 
                   for you since 1907:
 
                   for you since 1907:
 
+
 
                   { foreach $item (@items) {
 
                   { foreach $item (@items) {
 
                       $item_no++;
 
                       $item_no++;
Line 313: Line 314:  
                     }
 
                     }
 
                   }
 
                   }
 
+
 
                   Signed,
 
                   Signed,
 
                   Lord High Chamberlain
 
                   Lord High Chamberlain
 
+
 
We want to pass in an array which will be assigned to the array @items.  Here’s how to do that:
 
We want to pass in an array which will be assigned to the array @items.  Here’s how to do that:
   Line 423: Line 424:  
The "BROKEN" function could also use the "BROKEN_ARG" as a reference to store an error message or some other information that it wants to communicate back to the caller.  For example:
 
The "BROKEN" function could also use the "BROKEN_ARG" as a reference to store an error message or some other information that it wants to communicate back to the caller.  For example:
 
                 $error = ’’;
 
                 $error = ’’;
 
+
 
                   sub my_broken {
 
                   sub my_broken {
 
                       my %args = @_;
 
                       my %args = @_;
Line 431: Line 432:  
                       return undef;
 
                       return undef;
 
                   }
 
                   }
 
+
 
                   $template->fill_in(BROKEN => \&my_broken,
 
                   $template->fill_in(BROKEN => \&my_broken,
 
                                       BROKEN_ARG => \$error,
 
                                       BROKEN_ARG => \$error,
 
                                     );
 
                                     );
 
+
 
                   if ($error) {
 
                   if ($error) {
 
                     die "It didn’t work: $error";
 
                     die "It didn’t work: $error";
Line 478: Line 479:  
               $Q::amount = 141.61;
 
               $Q::amount = 141.61;
 
               $Q::part = ’hyoid bone’;
 
               $Q::part = ’hyoid bone’;
 
+
 
               $text = Text::Template->fill_this_in( <<’EOM’, PACKAGE => Q);
 
               $text = Text::Template->fill_this_in( <<’EOM’, PACKAGE => Q);
 
               Dear {$name},
 
               Dear {$name},
Line 490: Line 491:     
"fill_this_in" is a deprecated feature.  It is only here for backwards compatibility, and may be removed in some far-future version in "Text::Template".  You should use "fill_in_string" instead.  It is described in the next section.
 
"fill_this_in" is a deprecated feature.  It is only here for backwards compatibility, and may be removed in some far-future version in "Text::Template".  You should use "fill_in_string" instead.  It is described in the next section.
 +
 
===== "fill_in_string" =====
 
===== "fill_in_string" =====
   Line 621: Line 623:  
                 ...
 
                 ...
 
               }
 
               }
 
+
 
 
               ...
 
               ...
 
+
 
               { # we forgot to put ‘use strict’ here
 
               { # we forgot to put ‘use strict’ here
 
                 my $result = $boo + 12;    # $boo is misspelled and should be $foo
 
                 my $result = $boo + 12;    # $boo is misspelled and should be $foo
Line 647: Line 649:  
                 ...
 
                 ...
 
               }
 
               }
 
+
 
               ...
 
               ...
 
+
 
               { my $result = $boo + 12;    # $boo is misspelled and should be $foo
 
               { my $result = $boo + 12;    # $boo is misspelled and should be $foo
 
                 ...
 
                 ...
Line 695: Line 697:  
Jennifer is worried about the braces in the JavaScript being taken as the delimiters of the Perl program fragments.  Of course, disaster will ensue when perl tries to evaluate these as if they were Perl programs. The best choice is to find some unambiguous delimiter strings that you can use in your template instead of curly braces, and then use the "DELIMITERS" option.  However, if you can’t do this for some reason, there are  two easy workarounds:
 
Jennifer is worried about the braces in the JavaScript being taken as the delimiters of the Perl program fragments.  Of course, disaster will ensue when perl tries to evaluate these as if they were Perl programs. The best choice is to find some unambiguous delimiter strings that you can use in your template instead of curly braces, and then use the "DELIMITERS" option.  However, if you can’t do this for some reason, there are  two easy workarounds:
   −
You can put "\" in front of "{", "}", or "\" to remove its special
+
You can put "\" in front of "{", "}", or "\" to remove its special meaning.  So, for example, instead of
      meaning.  So, for example, instead of
      
                   if (br== "n3") {
 
                   if (br== "n3") {
Line 818: Line 819:     
               use Text::Template ’TTerror’;
 
               use Text::Template ’TTerror’;
 
+
 
               my $template = new Text::Template (SOURCE => $filename);
 
               my $template = new Text::Template (SOURCE => $filename);
 
               unless ($template) {
 
               unless ($template) {
Line 828: Line 829:     
               use Text::Template;
 
               use Text::Template;
 
+
 
 
               my $template = new Text::Template (SOURCE => $filename)
 
               my $template = new Text::Template (SOURCE => $filename)
 
                 or die "Couldn’t make template: $Text::Template::ERROR; aborting";
 
                 or die "Couldn’t make template: $Text::Template::ERROR; aborting";
Line 902: Line 903:     
[http://wiki.contribs.org/Esmith::templates Esmith::templates]
 
[http://wiki.contribs.org/Esmith::templates Esmith::templates]
[[Category:Howto]]
+
 
 
[[Category:SME Server Development Framework]]
 
[[Category:SME Server Development Framework]]
 
[[Category:Development Tools]]
 
[[Category:Development Tools]]
 
[[Category:SME9-Development]]
 
[[Category:SME9-Development]]

Navigation menu