Perl template cpan




















An optional arrayref holding the pair of delimiters used by the template. Context to use when rendering if not provided as a parameter to render. Defaults to the object itself. An optional hashref of partials to assign to the object. See the method partials for more details on its format. Returns the rendered template, given the optionally provided context. Uses the object's context attribute if not provided.

If the value is a coderef, it will be invoked to generate the value to be inserted in the template. If you want the value returned by the coderef to be interpolated as a Mustache template, a helper function is passed as the last argument to the coderef. If your template is going to generate a lot of text that you are just going to print out again anyway, you can save memory by having Text::Template print out the text as it is generated instead of making it into a big string and returning the string.

The generated text will be printed to this filehandle as it is constructed. This may result in the output appearing more quickly than it would have otherwise. You can have some Perl code prepended automatically to the beginning of every program fragment. If this option is present, its value should be a reference to a list of two strings. This is useful if you want to fill in the same template more than once. In some programs, this can be cumbersome. It constructs the template object for you, fills it in as specified, and returns the results.

It is only here for backwards compatibility, and may be removed in some far-future version in Text::Template. It is described in the next section. But I made the mistake four years ago and it is too late to change it. The filename is the name of the file that contains the template you want to fill in. It returns the result text. It will be a lot faster because it only has to read and parse the file once.

People always ask for this. The short answer is this is Perl, and Perl already has an include function. If you want it, you can just put. 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. If that is too verbose, here is a trick. Then in the main program, write. From then on, any template that you fill in with package Q can say. Suppose you don't want to insert a plain text file, but rather you want to include one template within another?

The text The King doesn't get into the form letter. Why not? If that's not the behavior you want, don't use my. Nevertheless, there's really no way except with Safe to protect against a template that says. If you're worried, or you can't trust the person who wrote the template, use the SAFE option.

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. 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. Note that these delimiters are literal strings , not regexes.

I tried for regexes, but it complicates the lexical analysis too much. 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:.

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:.

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.

Text::Template version 1. You can specify that any text at all be automatically added to the beginning of each program fragment. There are three 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.

Finally, you can make the class method call. Any variable referenced in the template that is not in the HASH option will be an error. Normally there are three places that prepended text could come from. 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. Suppose S is a subclass of Text::Template. This means that objects in class Derived will be affected by. Your derived class can override this method to get an arbitrary effect. 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. However, if you can't do this for some reason, there are two easy workarounds:.

So, for example, instead of. 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:. So if we wrote. This option allows you to inherit the parameter values from other objects. The only requirement for the other object is that it have a param method that works like HTML::Template's param.

A good candidate would be a CGI query object. Parameters you set directly take precedence over associated parameters. You can specify multiple objects to associate by passing an anonymous array to the associate option. They are searched for parameters in the order they appear:. NOTE : The parameter names are matched in a case-insensitive manner. When this parameter is set to true it is false by default extra variables that depend on the loop's context are made available inside a loop.

These are:. One use of this feature is to provide a "separator" similar in effect to the perl function join. Given an appropriate param call, of course. This can be used to make opening untrusted templates slightly less dangerous. Set this variable to determine the maximum depth that includes can reach. Set to 10 by default. Including files to a depth greater than this value causes an error message to be displayed. Set to 0 to disable this protection. This defaults to true. Normally variables declared outside a loop are not available inside a loop.

This is necessary to allow inner loops to access values set for outer loops that don't directly use the value. This option allows you to specify a filter for your template files.

A filter is a subroutine that will be called after HTML::Template reads your template file but before it starts parsing template tags. In the most simple usage, you simply assign a code reference to the filter parameter. This subroutine will receive a single argument - a reference to a string containing the template file text. Here is an example that accepts templates with tags that look like!!! More complicated usages are possible. You can request that your filter receives the template text as an array of lines rather than as a single scalar.

To do that you need to specify your filter using a hash-ref. In this form you specify the filter using the sub key and the desired argument format using the format key. The available formats are scalar and array. Using the array format will incur a performance penalty but may be more convenient in some situations. You may also have multiple filters. This allows simple filters to be combined for more elaborate functionality.

To do this you specify an array of filters. The filters are applied in the order they are specified. Set this parameter to a valid escape type see the escape option and HTML::Template will apply the specified escaping to all variables unless they declare a different escape in the template.

For instance, if you want to set the utf8 flag to always be on for every template loaded by this process you would do:. Any configuration options that are valid for new are acceptable to be passed to this method. In most situations you'll want to print this, like:. If a named parameter is unset it is simply replaced with ''. Calling output is guaranteed not to change the state of the HTML::Template object, in case you were wondering.

The Template module delegates most of the effort of processing templates to an underlying Template::Service object. This method returns a reference to that object. The Template::Service module uses a core Template::Context object for runtime processing of templates.

This method is a simple wrapper around the Template::Context method of the same name. It returns a compiled template for the source provided as an argument. The following list gives a short summary of each Template Toolkit configuration option.

See Template::Manual::Config for full details. Root of directory in which compiled template files should be written default: undef - don't compile. Flag to indicate regular Perl modules should be loaded if a named plugin can't be found default: 0. Reference to a custom service object default: Template::Service. Reference to a custom context object default: Template::Context. Reference to a custom stash object default: Template::Stash.

Reference to a custom parser object default: Template::Parser. Reference to a custom grammar object default: Template::Grammar.



0コメント

  • 1000 / 1000