Changes

Jump to navigation Jump to search
5 bytes added ,  00:03, 22 December 2013
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, ...);
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