Line 687: |
Line 687: |
| | | |
| ===== JavaScript ===== | | ===== JavaScript ===== |
− | | + | Jennifer D. St Clair asks: |
− | Jennifer D. St Clair asks:
| |
| | | |
| > Most of my pages contain JavaScript and Stylesheets. | | > Most of my pages contain JavaScript and Stylesheets. |
| > How do I change the template identifier? | | > How do I change the template identifier? |
| | | |
− | Jennifer is worried about the braces in the JavaScript being taken as
| + | 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: |
− | 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:
| |
| | | |
− | 1. 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 |
| | | |
Line 708: |
Line 701: |
| } | | } |
| | | |
− | you can put
| + | you can put |
| | | |
| if (br== "n3") \{ | | if (br== "n3") \{ |
Line 714: |
Line 707: |
| \} | | \} |
| | | |
− | and it’ll come out of the template engine the way you want.
| + | and it’ll come out of the template engine the way you want. |
| | | |
− | But here is another method that is probably better. To see how it
| + | But here is another method that is probably better. To see how it works, first consider what happens if you put this into a template: |
− | works, first consider what happens if you put this into a template:
| |
| | | |
| { ’foo’ } | | { ’foo’ } |
| | | |
− | Since it’s in braces, it gets evaluated, and obviously, this is going
| + | Since it’s in braces, it gets evaluated, and obviously, this is going to turn into |
− | to turn into
| |
| | | |
| foo | | foo |
| | | |
− | So now here’s the trick: In Perl, "q{...}" is the same as '...'. So if
| + | So now here’s the trick: In Perl, "q{...}" is the same as '...'. So if we wrote |
− | we wrote
| |
| | | |
| {q{foo}} | | {q{foo}} |
| | | |
− | it would turn into
| + | it would turn into |
| | | |
| foo | | foo |
| | | |
− | So for your JavaScript, just write
| + | So for your JavaScript, just write |
| | | |
| {q{if (br== "n3") { | | {q{if (br== "n3") { |
Line 742: |
Line 732: |
| } | | } |
| | | |
− | and it’ll come out as
| + | and it’ll come out as |
| | | |
| if (br== "n3") { | | if (br== "n3") { |
Line 748: |
Line 738: |
| } | | } |
| | | |
− | which is what you want.
| + | which is what you want. |
| | | |
− | Shut Up!
| + | ===== Shut Up! ===== |
| | | |
− | People sometimes try to put an initialization section at the top of
| + | People sometimes try to put an initialization section at the top of their templates, like this: |
− | their templates, like this:
| |
| | | |
| { ... | | { ... |
Line 759: |
Line 748: |
| } | | } |
| | | |
− | Then they complain because there is a 17 at the top of the output that
| + | Then they complain because there is a 17 at the top of the output that they didn’t want to have there. |
− | they didn’t want to have there.
| |
| | | |
− | Remember that a program fragment is replaced with its own return value,
| + | Remember that a program fragment is replaced with its own return value, and that in Perl the return value of a code block is the value of the last expression that was evaluated, which in this case is 17. If it didn’t do that, you wouldn’t be able to write "{$recipient}" and have the recipient filled in. |
− | and that in Perl the return value of a code block is the value of the
| |
− | last expression that was evaluated, which in this case is 17. If it
| |
− | didn’t do that, you wouldn’t be able to write "{$recipient}" and have
| |
− | the recipient filled in.
| |
| | | |
− | To prevent the 17 from appearing in the output is very simple:
| + | To prevent the 17 from appearing in the output is very simple: |
| | | |
| { ... | | { ... |
Line 775: |
Line 759: |
| } | | } |
| | | |
− | Now the last expression evaluated yields the empty string, which is
| + | Now the last expression evaluated yields the empty string, which is invisible. If you don’t like the way this looks, use |
− | invisible. If you don’t like the way this looks, use
| |
| | | |
| { ... | | { ... |
Line 783: |
Line 766: |
| } | | } |
| | | |
− | instead. Presumably, $SILENTLY has no value, so nothing will be inter-
| + | instead. Presumably, $SILENTLY has no value, so nothing will be interpolated. This is what is known as a ‘trick’. |
− | polated. This is what is known as a ‘trick’.
| |
| | | |
− | Compatibility
| + | ===== Compatibility ===== |
| | | |
− | Every effort has been made to make this module compatible with older
| + | Every effort has been made to make this module compatible with older versions. The only known exceptions follow: |
− | versions. The only known exceptions follow:
| |
| | | |
− | The output format of the default "BROKEN" subroutine has changed twice,
| + | The output format of the default "BROKEN" subroutine has changed twice, most recently between versions 1.31 and 1.40. |
− | most recently between versions 1.31 and 1.40.
| |
| | | |
− | Starting in version 1.10, the $OUT variable is arrogated for a special
| + | Starting in version 1.10, the $OUT variable is arrogated for a special meaning. If you had templates before version 1.10 that happened to use a variable named $OUT, you will have to change them to use some other variable or all sorts of strangeness will result. |
− | meaning. If you had templates before version 1.10 that happened to use
| |
− | a variable named $OUT, you will have to change them to use some other
| |
− | variable or all sorts of strangeness will result.
| |
| | | |
− | Between versions 0.1b and 1.00 the behavior of the \ metacharacter
| + | Between versions 0.1b and 1.00 the behavior of the \ metacharacter changed. In 0.1b, \\ was special everywhere, and the template processor always replaced it with a single backslash before passing the code to Perl for evaluation. The rule now is more complicated but probably more convenient. See the section on backslash processing, below, for a full discussion. |
− | changed. In 0.1b, \\ was special everywhere, and the template proces-
| |
− | sor always replaced it with a single backslash before passing the code
| |
− | to Perl for evaluation. The rule now is more complicated but probably
| |
− | more convenient. See the section on backslash processing, below, for a
| |
− | full discussion.
| |
| | | |
− | Backslash Processing
| + | ===== Backslash Processing ===== |
| | | |
− | In "Text::Template" beta versions, the backslash was special whenever
| + | In "Text::Template" beta versions, the backslash was special whenever it appeared before a brace or another backslash. That meant that while "{"\n"}" did indeed generate a newline, "{"\\"}" did not generate a backslash, because the code passed to Perl for evaluation was "\" which is a syntax error. If you wanted a backslash, you would have had to write "{"\\\\"}". |
− | it appeared before a brace or another backslash. That meant that while
| |
− | "{"\n"}" did indeed generate a newline, "{"\\"}" did not generate a
| |
− | backslash, because the code passed to Perl for evaluation was "\" which
| |
− | is a syntax error. If you wanted a backslash, you would have had to
| |
− | write "{"\\\\"}".
| |
| | | |
− | In "Text::Template" versions 1.00 through 1.10, there was a bug: Back-
| + | In "Text::Template" versions 1.00 through 1.10, there was a bug: Backslash was special everywhere. In these versions, "{"\n"}" generated the letter "n". |
− | slash was special everywhere. In these versions, "{"\n"}" generated
| |
− | the letter "n".
| |
| | | |
− | The bug has been corrected in version 1.11, but I did not go back to
| + | The bug has been corrected in version 1.11, but I did not go back to exactly the old rule, because I did not like the idea of having to write "{"\\\\"}" to get one backslash. The rule is now more complicated to remember, but probably easier to use. The rule is now: Backslashes are always passed to Perl unchanged unless they occur as part of a sequence like "\\\\\\{" or "\\\\\\}". In these contexts, they are special; "\\" is replaced with "\", and "\{" and "\}" signal a literal brace. |
− | exactly the old rule, because I did not like the idea of having to
| |
− | write "{"\\\\"}" to get one backslash. The rule is now more compli-
| |
− | cated to remember, but probably easier to use. The rule is now: Back-
| |
− | slashes are always passed to Perl unchanged unless they occur as part
| |
− | of a sequence like "\\\\\\{" or "\\\\\\}". In these contexts, they are
| |
− | special; "\\" is replaced with "\", and "\{" and "\}" signal a literal
| |
− | brace.
| |
| | | |
− | Examples:
| + | Examples: |
| | | |
| \{ foo \} | | \{ foo \} |
| | | |
− | is not evaluated, because the "\" before the braces signals that they
| + | is not evaluated, because the "\" before the braces signals that they should be taken literally. The result in the output looks like this: |
− | should be taken literally. The result in the output looks like this:
| |
| | | |
| { foo } | | { foo } |
| | | |
− | This is a syntax error:
| + | This is a syntax error: |
| | | |
| { "foo}" } | | { "foo}" } |
| | | |
− | because "Text::Template" thinks that the code ends at the first "}",
| + | because "Text::Template" thinks that the code ends at the first "}", and then gets upset when it sees the second one. To make this work correctly, use |
− | and then gets upset when it sees the second one. To make this work
| |
− | correctly, use
| |
| | | |
| { "foo\}" } | | { "foo\}" } |
| | | |
− | This passes "foo}" to Perl for evaluation. Note there’s no "\" in the
| + | This passes "foo}" to Perl for evaluation. Note there’s no "\" in the evaluated code. If you really want a "\" in the evaluated code, use |
− | evaluated code. If you really want a "\" in the evaluated code, use
| |
| | | |
| { "foo\\\}" } | | { "foo\\\}" } |
| | | |
− | This passes "foo\}" to Perl for evaluation.
| + | This passes "foo\}" to Perl for evaluation. |
| | | |
− | Starting with "Text::Template" version 1.20, backslash processing is
| + | Starting with "Text::Template" version 1.20, backslash processing is disabled if you use the "DELIMITERS" option to specify alternative delimiter strings. |
− | disabled if you use the "DELIMITERS" option to specify alternative
| |
− | delimiter strings.
| |
| | | |
− | A short note about $Text::Template::ERROR
| + | ===== A short note about $Text::Template::ERROR ===== |
| | | |
− | In the past some people have fretted about ‘violating the package
| + | In the past some people have fretted about ‘violating the package boundary’ by examining a variable inside the "Text::Template" package. Don’t feel this way. $Text::Template::ERROR is part of the published, official interface to this package. It is perfectly OK to inspect this variable. The interface is not going to change. |
− | boundary’ by examining a variable inside the "Text::Template" package.
| |
− | Don’t feel this way. $Text::Template::ERROR is part of the published,
| |
− | official interface to this package. It is perfectly OK to inspect this
| |
− | variable. The interface is not going to change.
| |
| | | |
− | If it really, really bothers you, you can import a function called
| + | If it really, really bothers you, you can import a function called "TTerror" that returns the current value of the $ERROR variable. So you can say: |
− | "TTerror" that returns the current value of the $ERROR variable. So
| |
− | you can say:
| |
| | | |
| use Text::Template ’TTerror’; | | use Text::Template ’TTerror’; |
Line 878: |
Line 824: |
| } | | } |
| | | |
− | I don’t see what benefit this has over just doing this:
| + | I don’t see what benefit this has over just doing this: |
| | | |
| use Text::Template; | | use Text::Template; |
Line 885: |
Line 831: |
| or die "Couldn’t make template: $Text::Template::ERROR; aborting"; | | or die "Couldn’t make template: $Text::Template::ERROR; aborting"; |
| | | |
− | But if it makes you happy to do it that way, go ahead.
| + | But if it makes you happy to do it that way, go ahead. |
| | | |
− | Sticky Widgets in Template Files
| + | ===== Sticky Widgets in Template Files ===== |
| | | |
− | The "CGI" module provides functions for ‘sticky widgets’, which are
| + | The "CGI" module provides functions for ‘sticky widgets’, which are form input controls that retain their values from one page to the next. Sometimes people want to know how to include these widgets into their template output. |
− | form input controls that retain their values from one page to the next.
| |
− | Sometimes people want to know how to include these widgets into their
| |
− | template output.
| |
| | | |
− | It’s totally straightforward. Just call the "CGI" functions from
| + | It’s totally straightforward. Just call the "CGI" functions from inside the template: |
− | inside the template:
| |
| | | |
| { $q->checkbox_group(NAME => ’toppings’, | | { $q->checkbox_group(NAME => ’toppings’, |
Line 904: |
Line 846: |
| } | | } |
| | | |
− | Automatic preprocessing of program fragments
| + | ===== Automatic preprocessing of program fragments ===== |
| | | |
− | It may be useful to preprocess the program fragments before they are
| + | It may be useful to preprocess the program fragments before they are evaluated. See "Text::Template::Preprocess" for more details. |
− | evaluated. See "Text::Template::Preprocess" for more details.
| |
| | | |
− | Author
| + | ===== Author ===== |
| | | |
− | Mark-Jason Dominus, Plover Systems
| + | Mark-Jason Dominus, Plover Systems |
| | | |
− | Please send questions and other remarks about this software to
| + | Please send questions and other remarks about this software to "mjd-perl-template+@plover.com" |
− | "mjd-perl-template+@plover.com"
| |
| | | |
− | You can join a very low-volume (<10 messages per year) mailing list for
| + | You can join a very low-volume (<10 messages per year) mailing list for announcements about this package. Send an empty note to "mjd-perl-tem-plate-request@plover.com" to join. |
− | announcements about this package. Send an empty note to "mjd-perl-tem-
| |
− | plate-request@plover.com" to join.
| |
| | | |
− | For updates, visit "http://www.plover.com/~mjd/perl/Template/".
| + | For updates, visit "http://www.plover.com/~mjd/perl/Template/". |
| | | |
− | Support?
| + | ===== Support? ===== |
| | | |
− | This software is version 1.45. It may have bugs. Suggestions and bug
| + | This software is version 1.45. It may have bugs. Suggestions and bug reports are always welcome. Send them to "mjd-perl-tem-plate+@plover.com". (That is my address, not the address of the mailing list. The mailing list address is a secret.) |
− | reports are always welcome. Send them to "mjd-perl-tem-
| |
− | plate+@plover.com". (That is my address, not the address of the mail-
| |
− | ing list. The mailing list address is a secret.)
| |
| | | |
| == LICENSE == | | == LICENSE == |
− | Text::Template version 1.45
| + | Text::Template version 1.45 |
− | Copyright (C) 2008 Mark Jason Dominus
| + | Copyright (C) 2008 Mark Jason Dominus |
| | | |
− | This program is free software; you can redistribute it and/or
| + | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. You may also can redistribute it and/or modify it under the terms of the Perl Artistic License. |
− | modify it under the terms of the GNU General Public License as
| |
− | published by the Free Software Foundation; either version 2 of the
| |
− | License, or (at your option) any later version. You may also can
| |
− | redistribute it and/or modify it under the terms of the Perl
| |
− | Artistic License.
| |
| | | |
− | This program is distributed in the hope that it will be useful,
| + | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
− | but WITHOUT ANY WARRANTY; without even the implied warranty of
| |
− | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| |
− | GNU General Public License for more details.
| |
| | | |
− | You should have received copies of the GNU General Public License
| + | You should have received copies of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
− | along with this program; if not, write to the Free Software
| |
− | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
| |
| | | |
| == THANKS == | | == THANKS == |
− | Many thanks to the following people for offering support, encourage-
| + | Many thanks to the following people for offering support, encouragement, advice, bug reports, and all the other good stuff. |
− | ment, advice, bug reports, and all the other good stuff.
| |
| | | |
− | David H. Adler / Joel Appelbaum / Klaus Arnhold / Antonio Araga~o /
| + | David H. Adler / Joel Appelbaum / Klaus Arnhold / Antonio Araga~o / Kevin Atteson / Chris.Brezil / Mike Brodhead / Tom Brown / Dr. Frank Bucolo / Tim Bunce / Juan E. Camacho / Itamar Almeida de Carvalho / Joseph Cheek / Gene Damon / San Deng / Bob Dougherty / Marek Grac / Dan Franklin / gary at dls.net / Todd A. Green / Donald L. Greer Jr. / Michelangelo Grigni / Zac Hansen / Tom Henry / Jarko Hietaniemi / Matt X. Hunter / Robert M. Ioffe / Daniel LaLiberte / Reuven M. Lerner / Trip Lilley / Yannis Livassof / Val Luck / Kevin Madsen / David Marshall / James Mastros / Joel Meulenberg / Jason Moore / Sergey Myasnikov / Chris Nandor / Bek Oberin / Steve Palincsar / Ron Pero / Hans Persson / Sean Roehnelt / Jonathan Roy / Shabbir J. Safdar / Jennifer D. St Clair / Uwe Schneider / Randal L. Schwartz / Michael G Schwern / Yonat Sharon / Brian C. Shensky / Niklas Skoglund / Tom Snee / Fred Steinberg / Hans Stoop / Michael J. Suzio / Dennis Taylor / James H.Thompson / Shad Todd / Lieven Tomme / Lorenzo Valdettaro / Larry Virden / Andy Wardley / Archie Warnock / Chris Wesley / Matt Womer / Andrew G Wood / Daini Xie / Michaely Yeung |
− | Kevin Atteson / Chris.Brezil / Mike Brodhead / Tom Brown / Dr. Frank
| |
− | Bucolo / Tim Bunce / Juan E. Camacho / Itamar Almeida de Carvalho /
| |
− | Joseph Cheek / Gene Damon / San Deng / Bob Dougherty / Marek Grac / Dan
| |
− | Franklin / gary at dls.net / Todd A. Green / Donald L. Greer Jr. /
| |
− | Michelangelo Grigni / Zac Hansen / Tom Henry / Jarko Hietaniemi / Matt
| |
− | X. Hunter / Robert M. Ioffe / Daniel LaLiberte / Reuven M. Lerner /
| |
− | Trip Lilley / Yannis Livassof / Val Luck / Kevin Madsen / David Mar-
| |
− | shall / James Mastros / Joel Meulenberg / Jason Moore / Sergey Myas-
| |
− | nikov / Chris Nandor / Bek Oberin / Steve Palincsar / Ron Pero / Hans
| |
− | Persson / Sean Roehnelt / Jonathan Roy / Shabbir J. Safdar / Jennifer
| |
− | D. St Clair / Uwe Schneider / Randal L. Schwartz / Michael G Schwern /
| |
− | Yonat Sharon / Brian C. Shensky / Niklas Skoglund / Tom Snee / Fred
| |
− | Steinberg / Hans Stoop / Michael J. Suzio / Dennis Taylor / James H.
| |
− | Thompson / Shad Todd / Lieven Tomme / Lorenzo Valdettaro / Larry Virden
| |
− | / Andy Wardley / Archie Warnock / Chris Wesley / Matt Womer / Andrew G
| |
− | Wood / Daini Xie / Michaely Yeung
| |
| | | |
− | Special thanks to:
| + | Special thanks to: |
| | | |
− | Jonathan Roy
| + | Jonathan Roy |
− | for telling me how to do the "Safe" support (I spent two years worry-
| + | for telling me how to do the "Safe" support (I spent two years worrying about it, and then Jonathan pointed out that it was trivial.) |
− | ing about it, and then Jonathan pointed out that it was trivial.)
| |
| | | |
− | Ranjit Bhatnagar
| + | Ranjit Bhatnagar |
− | for demanding less verbose fragments like they have in ASP, for help-
| + | for demanding less verbose fragments like they have in ASP, for helping me figure out the Right Thing, and, especially, for talking me out of adding any new syntax. These discussions resulted in the $OUT feature. |
− | ing me figure out the Right Thing, and, especially, for talking me
| |
− | out of adding any new syntax. These discussions resulted in the $OUT
| |
− | feature.
| |
| | | |
− | Bugs and Caveats
| + | Bugs and Caveats |
| | | |
− | "my" variables in "fill_in" are still susceptible to being clobbered by
| + | "my" variables in "fill_in" are still susceptible to being clobbered by template evaluation. They all begin with "fi_", so avoid those names in your templates. |
− | template evaluation. They all begin with "fi_", so avoid those names
| |
− | in your templates.
| |
| | | |
− | The line number information will be wrong if the template’s lines are
| + | The line number information will be wrong if the template’s lines are not terminated by "\n". You should let me know if this is a problem. If you do, I will fix it. |
− | not terminated by "\n". You should let me know if this is a problem.
| |
− | If you do, I will fix it.
| |
| | | |
− | The $OUT variable has a special meaning in templates, so you cannot use
| + | The $OUT variable has a special meaning in templates, so you cannot use it as if it were a regular variable. |
− | it as if it were a regular variable.
| |
| | | |
− | There are not quite enough tests in the test suite.
| + | There are not quite enough tests in the test suite. |