Changes

Jump to navigation Jump to search
1,103 bytes removed ,  16:56, 20 December 2013
no edit summary
Line 465: Line 465:  
If you specify "DELIMITERS" in the call to "fill_in", they override any delimiters you set when you created the template object with "new".
 
If you specify "DELIMITERS" in the call to "fill_in", they override any delimiters you set when you created the template object with "new".
   −
Convenience Functions
+
==== Convenience Functions ====
      "fill_this_in"
+
===== "fill_this_in" =====
   −
      The basic way to fill in a template is to create a template object and
+
The basic way to fill in a template is to create a template object and then call "fill_in" on it.  This is useful if you want to fill in the same template more than once.
      then call "fill_in" on it.  This is useful if you want to fill in the
  −
      same template more than once.
     −
      In some programs, this can be cumbersome.  "fill_this_in" accepts a
+
In some programs, this can be cumbersome.  "fill_this_in" accepts a string, which contains the template, and a list of options, which are passed to "fill_in" as above.  It constructs the template object for you, fills it in as specified, and returns the results.  It returns "undef" and sets $Text::Template::ERROR if it couldn’t generate any results.
      string, which contains the template, and a list of options, which are
  −
      passed to "fill_in" as above.  It constructs the template object for
  −
      you, fills it in as specified, and returns the results.  It returns
  −
      "undef" and sets $Text::Template::ERROR if it couldn’t generate any
  −
      results.
     −
      An example:
+
An example:
    
               $Q::name = ’Donald’;
 
               $Q::name = ’Donald’;
Line 493: Line 486:  
               EOM
 
               EOM
   −
      Notice how we included the template in-line in the program by using a
+
Notice how we included the template in-line in the program by using a ‘here document’ with the "<<" notation.
      ‘here document’ with the "<<" notation.
     −
      "fill_this_in" is a deprecated feature.  It is only here for backwards
+
"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.
      compatibility, and may be removed in some far-future version in
+
===== "fill_in_string" =====
      "Text::Template".  You should use "fill_in_string" instead.  It is
  −
      described in the next section.
  −
    "fill_in_string"
     −
      It is stupid that "fill_this_in" is a class method.  It should have
+
It is stupid that "fill_this_in" is a class method.  It should have been just an imported function, so that you could omit the "Text::Template->" in the example above.  But I made the mistake four years ago and it is too late to change it.
      been just an imported function, so that you could omit the "Text::Tem-
  −
      plate->" in the example above.  But I made the mistake four years ago
  −
      and it is too late to change it.
     −
      "fill_in_string" is exactly like "fill_this_in" except that it is not a
+
"fill_in_string" is exactly like "fill_this_in" except that it is not a method and you can omit the "Text::Template->" and just say
      method and you can omit the "Text::Template->" and just say
      
               print fill_in_string(<<’EOM’, ...);
 
               print fill_in_string(<<’EOM’, ...);
Line 515: Line 500:  
               EOM
 
               EOM
   −
      To use "fill_in_string", you need to say
+
To use "fill_in_string", you need to say
    
               use Text::Template ’fill_in_string’;
 
               use Text::Template ’fill_in_string’;
   −
      at the top of your program.  You should probably use "fill_in_string"
+
at the top of your program.  You should probably use "fill_in_string" instead of "fill_this_in".
      instead of "fill_this_in".
      
       "fill_in_file"
 
       "fill_in_file"
 
+
If you import "fill_in_file", you can say
      If you import "fill_in_file", you can say
      
               $text = fill_in_file(filename, ...);
 
               $text = fill_in_file(filename, ...);
   −
      The "..." are passed to "fill_in" as above.  The filename is the name
+
The "..." are passed to "fill_in" as above.  The filename is the name of the file that contains the template you want to fill in.  It returns the result text. or "undef", as usual.
      of the file that contains the template you want to fill in.  It returns
  −
      the result text. or "undef", as usual.
     −
      If you are going to fill in the same file more than once in the same
+
If you are going to fill in the same file more than once in the same program you should use the longer "new" / "fill_in" sequence instead. It will be a lot faster because it only has to read and parse the file once.
      program you should use the longer "new" / "fill_in" sequence instead.
  −
      It will be a lot faster because it only has to read and parse the file
  −
      once.
     −
      Including files into templates
+
Including files into templates
   −
      People always ask for this.  ‘‘Why don’t you have an include func-
+
People always ask for this.  ‘‘Why don’t you have an include function?’’ they want to know.  The short answer is this is Perl, and Perl already has an include function.  If you want it, you can just put
      tion?’’ they want to know.  The short answer is this is Perl, and Perl
  −
      already has an include function.  If you want it, you can just put
      
               {qx{cat filename}}
 
               {qx{cat filename}}
   −
      into your template.  VoilA.
+
into your template.  VoilA.
  If you don’t want to use "cat", you can write a little four-line func-
+
 
      tion that opens a file and dumps out its contents, and call it from the
+
If you don’t want to use "cat", you can write a little four-line function that opens a file and dumps out its contents, and call it from the template.  I wrote one for you.  In the template, you can say
      template.  I wrote one for you.  In the template, you can say
      
               {Text::Template::_load_text(filename)}
 
               {Text::Template::_load_text(filename)}
   −
      If that is too verbose, here is a trick.  Suppose the template package
+
If that is too verbose, here is a trick.  Suppose the template package that you are going to be mentioning in the "fill_in" call is package "Q".  Then in the main program, write
      that you are going to be mentioning in the "fill_in" call is package
  −
      "Q".  Then in the main program, write
      
               *Q::include = \&Text::Template::_load_text;
 
               *Q::include = \&Text::Template::_load_text;
   −
      This imports the "_load_text" function into package "Q" with the name
+
This imports the "_load_text" function into package "Q" with the name "include".  From then on, any template that you fill in with package "Q" can say
      "include".  From then on, any template that you fill in with package
  −
      "Q" can say
      
               {include(filename)}
 
               {include(filename)}
   −
      to insert the text from the named file at that point.  If you are using
+
to insert the text from the named file at that point.  If you are using the "HASH" option instead, just put "include => \&Text::Template::_load_text" into the hash instead of importing it explicitly.
      the "HASH" option instead, just put "include => \&Text::Tem-
  −
      plate::_load_text" into the hash instead of importing it explicitly.
     −
      Suppose you don’t want to insert a plain text file, but rather you want
+
Suppose you don’t want to insert a plain text file, but rather you want to include one template within another?  Just use "fill_in_file" in the template itself:
      to include one template within another?  Just use "fill_in_file" in the
  −
      template itself:
      
               {Text::Template::fill_in_file(filename)}
 
               {Text::Template::fill_in_file(filename)}
   −
      You can do the same importing trick if this is too much to type.
+
You can do the same importing trick if this is too much to type.
   −
Miscellaneous
+
==== Miscellaneous ====
      "my" variables
+
===== "my" variables =====
   −
      People are frequently surprised when this doesn’t work:
+
People are frequently surprised when this doesn’t work:
    
               my $recipient = ’The King’;
 
               my $recipient = ’The King’;
 
               my $text = fill_in_file(’formletter.tmpl’);
 
               my $text = fill_in_file(’formletter.tmpl’);
   −
      The text "The King" doesn’t get into the form letter.  Why not?
+
The text "The King" doesn’t get into the form letter.  Why not? Because $recipient is a "my" variable, and the whole point of "my" variables is that they’re private and inaccessible except in the scope in which they’re declared.  The template is not part of that scope, so the template can’t see $recipient.
      Because $recipient is a "my" variable, and the whole point of "my"
+
If that’s not the behavior you want, don’t use "my".  "my" means a private variable, and in this case you don’t want the variable to be private.  Put the variables into package variables in some other package, and use the "PACKAGE" option to "fill_in":
      variables is that they’re private and inaccessible except in the scope
  −
      in which they’re declared.  The template is not part of that scope, so
  −
      the template can’t see $recipient.
  −
      If that’s not the behavior you want, don’t use "my".  "my" means a pri-
  −
      vate variable, and in this case you don’t want the variable to be pri-
  −
      vate.  Put the variables into package variables in some other package,
  −
      and use the "PACKAGE" option to "fill_in":
      
               $Q::recipient = $recipient;
 
               $Q::recipient = $recipient;
 
               my $text = fill_in_file(’formletter.tmpl’, PACKAGE => ’Q’);
 
               my $text = fill_in_file(’formletter.tmpl’, PACKAGE => ’Q’);
   −
      or pass the names and values in a hash with the "HASH" option:
+
or pass the names and values in a hash with the "HASH" option:
    
               my $text = fill_in_file(’formletter.tmpl’, HASH => { recipient => $recipient });
 
               my $text = fill_in_file(’formletter.tmpl’, HASH => { recipient => $recipient });
   −
      Security Matters
+
===== Security Matters =====
 
+
All variables are evaluated in the package you specify with the "PACKAGE" option of "fill_in".  if you use this option, and if your templates don’t do anything egregiously stupid, you won’t have to worry that evaluation of the little programs will creep out into the rest of your program and wreck something.
      All variables are evaluated in the package you specify with the "PACK-
  −
      AGE" option of "fill_in".  if you use this option, and if your tem-
  −
      plates don’t do anything egregiously stupid, you won’t have to worry
  −
      that evaluation of the little programs will creep out into the rest of
  −
      your program and wreck something.
     −
      Nevertheless, there’s really no way (except with "Safe") to protect
+
Nevertheless, there’s really no way (except with "Safe") to protect against a template that says
      against a template that says
      
               { $Important::Secret::Security::Enable = 0;
 
               { $Important::Secret::Security::Enable = 0;
Line 616: Line 570:  
               }
 
               }
   −
      or
+
or
    
               { $/ = "ho ho ho";  # Sabotage future uses of <FH>.
 
               { $/ = "ho ho ho";  # Sabotage future uses of <FH>.
Line 622: Line 576:  
               }
 
               }
   −
      or even
+
or even
    
               { system("rm -rf /") }
 
               { system("rm -rf /") }
   −
      so don’t go filling in templates unless you’re sure you know what’s in
+
so don’t go filling in templates unless you’re sure you know what’s in them.  If you’re worried, or you can’t trust the person who wrote the template, use the "SAFE" option.
      them.  If you’re worried, or you can’t trust the person who wrote the
+
A final warning: program fragments run a small risk of accidentally clobbering local variables in the "fill_in" function itself.  These variables all have names that begin with $fi_, so if you stay away from those names you’ll be safe.  (Of course, if you’re a real wizard you can tamper with them deliberately for exciting effects; this is actually how $OUT works.)  I can fix this, but it will make the package slower to do it, so I would prefer not to.  If you are worried about this, send me mail and I will show you what to do about it.
      template, use the "SAFE" option.
  −
    A final warning: program fragments run a small risk of accidentally
  −
      clobbering local variables in the "fill_in" function itself.  These
  −
      variables all have names that begin with $fi_, so if you stay away from
  −
      those names you’ll be safe.  (Of course, if you’re a real wizard you
  −
      can tamper with them deliberately for exciting effects; this is actu-
  −
      ally how $OUT works.)  I can fix this, but it will make the package
  −
      slower to do it, so I would prefer not to.  If you are worried about
  −
      this, send me mail and I will show you what to do about it.
        −
Alternative Delimiters
+
===== Alternative Delimiters =====
   −
      Lorenzo Valdettaro pointed out that if you are using "Text::Template"
+
Lorenzo Valdettaro pointed out that if you are using "Text::Template" to generate TeX output, the choice of braces as the program fragment delimiters makes you suffer suffer suffer.  Starting in version 1.20, you can change the choice of delimiters to something other than curly braces.
      to generate TeX output, the choice of braces as the program fragment
  −
      delimiters makes you suffer suffer suffer.  Starting in version 1.20,
  −
      you can change the choice of delimiters to something other than curly
  −
      braces.
     −
      In either the "new()" call or the "fill_in()" call, you can specify an
+
In either the "new()" call or the "fill_in()" call, you can specify an alternative set of delimiters with the "DELIMITERS" option.  For example, if you would like code fragments to be delimited by "[@--" and "--@]" instead of "{" and "}", use
      alternative set of delimiters with the "DELIMITERS" option.  For exam-
  −
      ple, if you would like code fragments to be delimited by "[@--" and
  −
      "--@]" instead of "{" and "}", use
      
               ... DELIMITERS => [ ’[@--’, ’--@]’ ], ...
 
               ... DELIMITERS => [ ’[@--’, ’--@]’ ], ...
   −
      Note that these delimiters are literal strings, not regexes.  (I tried
+
Note that these delimiters are literal strings, not regexes.  (I tried for regexes, but it complicates the lexical analysis too much.)  Note also that "DELIMITERS" disables the special meaning of the backslash, so if you want to include the delimiters in the literal text of your template file, you are out of luck---it is up to you to choose delimiters that do not conflict with what you are doing.  The delimiter strings may still appear inside of program fragments as long as they
      for regexes, but it complicates the lexical analysis too much.)  Note
+
       nest properly.  This means that if for some reason you absolutely must have a program fragment that mentions one of the delimiters, like this:
      also that "DELIMITERS" disables the special meaning of the backslash,
  −
      so if you want to include the delimiters in the literal text of your
  −
      template file, you are out of luck---it is up to you to choose delim-
  −
      iters that do not conflict with what you are doing.  The delimiter
  −
      strings may still appear inside of program fragments as long as they
  −
       nest properly.  This means that if for some reason you absolutely must
  −
      have a program fragment that mentions one of the delimiters, like this:
      
               [@--
 
               [@--
Line 668: Line 599:  
               --@]
 
               --@]
   −
      you may be able to make it work by doing this instead:
+
you may be able to make it work by doing this instead:
    
               [@--
 
               [@--
Line 675: Line 606:  
               --@]
 
               --@]
   −
      It may be safer to choose delimiters that begin with a newline charac-
+
It may be safer to choose delimiters that begin with a newline character.
      ter.
     −
      Because the parsing of templates is simplified by the absence of back-
+
Because the parsing of templates is simplified by the absence of backslash escapes, using alternative "DELIMITERS" may speed up the parsing process by 20-25%.  This shows that my original choice of "{" and "}" was very bad.
      slash escapes, using alternative "DELIMITERS" may speed up the parsing
  −
      process by 20-25%.  This shows that my original choice of "{" and "}"
  −
      was very bad.
     −
      "PREPEND" feature and using "strict" in templates
+
===== "PREPEND" =====
 +
feature and using "strict" in templates
   −
      Suppose you would like to use "strict" in your templates to detect
+
Suppose you would like to use "strict" in your templates to detect undeclared variables and the like.  But each code fragment is a separate lexical scope, so you have to turn on "strict" at the top of each and every code fragment:
      undeclared variables and the like.  But each code fragment is a sepa-
  −
      rate lexical scope, so you have to turn on "strict" at the top of each
  −
      and every code fragment:
      
               { use strict;
 
               { use strict;
Line 703: Line 628:  
               }
 
               }
   −
      Because we didn’t put "use strict" at the top of the second fragment,
+
Because we didn’t put "use strict" at the top of the second fragment, it was only active in the first fragment, and we didn’t get any "strict" checking in the second fragment.  Then we mispelled $foo and the error wasn’t caught.
      it was only active in the first fragment, and we didn’t get any
  −
      "strict" checking in the second fragment.  Then we mispelled $foo and
  −
      the error wasn’t caught.
     −
      "Text::Template" version 1.22 and higher has a new feature to make this
+
"Text::Template" version 1.22 and higher has a new feature to make this easier.  You can specify that any text at all be automatically added to the beginning of each program fragment.
      easier.  You can specify that any text at all be automatically added to
  −
      the beginning of each program fragment.
     −
      When you make a call to "fill_in", you can specify a
+
When you make a call to "fill_in", you can specify a
    
               PREPEND => ’some perl statements here’
 
               PREPEND => ’some perl statements here’
   −
      option; the statements will be prepended to each program fragment for
+
option; the statements will be prepended to each program fragment for that one call only.  Suppose that the "fill_in" call included a
      that one call only.  Suppose that the "fill_in" call included a
      
               PREPEND => ’use strict;’
 
               PREPEND => ’use strict;’
   −
      option, and that the template looked like this:
+
option, and that the template looked like this:
    
               { use vars ’$foo’;
 
               { use vars ’$foo’;
Line 734: Line 653:  
               }
 
               }
   −
      The code in the second fragment would fail, because $boo has not been
+
The code in the second fragment would fail, because $boo has not been declared.  "use strict" was implied, even though you did not write it explicitly, because the "PREPEND" option added it for you automatically.
      declared.  "use strict" was implied, even though you did not write it
  −
      explicitly, because the "PREPEND" option added it for you automati-
  −
      cally.
     −
      There are two other ways to do this.  At the time you create the tem-
+
There are two other ways to do this.  At the time you create the template object with "new", you can also supply a "PREPEND" option, in which case the statements will be prepended each time you fill in that template.  If the "fill_in" call has its own "PREPEND" option, this overrides the one specified at the time you created the template.
      plate object with "new", you can also supply a "PREPEND" option, in
+
Finally, you can make the class method call
      which case the statements will be prepended each time you fill in that
  −
      template.  If the "fill_in" call has its own "PREPEND" option, this
  −
      overrides the one specified at the time you created the template.
  −
      Finally, you can make the class method call
      
               Text::Template->always_prepend(’perl statements’);
 
               Text::Template->always_prepend(’perl statements’);
   −
      If you do this, then call calls to "fill_in" for any template will
+
If you do this, then call calls to "fill_in" for any template will attach the perl statements to the beginning of each program fragment, except where overridden by "PREPEND" options to "new" or "fill_in".
      attach the perl statements to the beginning of each program fragment,
  −
      except where overridden by "PREPEND" options to "new" or "fill_in".
     −
      Prepending in Derived Classes
+
===== Prepending in Derived Classes =====
   −
      This section is technical, and you should skip it on the first few
+
This section is technical, and you should skip it on the first few readings.
      readings.
     −
      Normally there are three places that prepended text could come from.
+
Normally there are three places that prepended text could come from. It could come from the "PREPEND" option in the "fill_in" call, from the "PREPEND" option in the "new" call that created the template object, or from the argument of the "always_prepend" call.  "Text::Template" looks for these three things in order and takes the first one that it finds.
      It could come from the "PREPEND" option in the "fill_in" call, from the
  −
      "PREPEND" option in the "new" call that created the template object, or
  −
      from the argument of the "always_prepend" call.  "Text::Template" looks
  −
      for these three things in order and takes the first one that it finds.
     −
      In a subclass of "Text::Template", this last possibility is ambiguous.
+
In a subclass of "Text::Template", this last possibility is ambiguous. Suppose "S" is a subclass of "Text::Template".  Should
      Suppose "S" is a subclass of "Text::Template".  Should
      
               Text::Template->always_prepend(...);
 
               Text::Template->always_prepend(...);
   −
      affect objects in class "Derived"?  The answer is that you can have it
+
affect objects in class "Derived"?  The answer is that you can have it either way.
      either way.
     −
      The "always_prepend" value for "Text::Template" is normally stored in
+
The "always_prepend" value for "Text::Template" is normally stored in a hash variable named %GLOBAL_PREPEND under the key "Text::Template". When "Text::Template" looks to see what text to prepend, it first looks in the template object itself, and if not, it looks in $GLOBAL_PREPEND{class} where class is the class to which the template object belongs.  If it doesn’t find any value, it looks in $GLOBAL_PREPEND{'Text::Template'}.  This means that objects in class "Derived" will be affected by
      a hash variable named %GLOBAL_PREPEND under the key "Text::Template".
  −
      When "Text::Template" looks to see what text to prepend, it first looks
  −
      in the template object itself, and if not, it looks in
  −
      $GLOBAL_PREPEND{class} where class is the class to which the template
  −
      object belongs.  If it doesn’t find any value, it looks in
  −
      $GLOBAL_PREPEND{'Text::Template'}.  This means that objects in class
  −
      "Derived" will be affected by
      
               Text::Template->always_prepend(...);
 
               Text::Template->always_prepend(...);
   −
      unless there is also a call to
+
unless there is also a call to
    
               Derived->always_prepend(...);
 
               Derived->always_prepend(...);
   −
      So when you’re designing your derived class, you can arrange to have
+
So when you’re designing your derived class, you can arrange to have your objects ignore "Text::Template::always_prepend" calls by simply putting "Derived->always_prepend('')" at the top of your module.
      your objects ignore "Text::Template::always_prepend" calls by simply
  −
      putting "Derived->always_prepend('')" at the top of your module.
     −
      Of course, there is also a final escape hatch: Templates support a
+
Of course, there is also a final escape hatch: Templates support a "prepend_text" that is used to look up the appropriate text to be prepended at "fill_in" time.  Your derived class can override this method to get an arbitrary effect.
      "prepend_text" that is used to look up the appropriate text to be
  −
      prepended at "fill_in" time.  Your derived class can override this
  −
      method to get an arbitrary effect.
     −
      JavaScript
+
===== JavaScript =====
    
       Jennifer D. St Clair asks:
 
       Jennifer D. St Clair asks:

Navigation menu