Line 61: |
Line 61: |
| 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 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]] |