Template toolkit if else end




















The Template Toolkit is a collection of Perl modules which implement a fast, flexible, powerful and extensible template processing system. However, it is most often used for generating static and dynamic web content, so that's what we'll focus on here. Although the Template Toolkit is written in Perl, you don't need to be a Perl programmer to use it.

It was designed to allow non-programmers to easily create and maintain template-based web sites without having to mess around writing Perl code or going crazy with cut-n-paste. However, the Template Toolkit is also designed to be extremely flexible and extensible. If you are a Perl programmer, or know someone who is, then you can easily hook the Template Toolkit into your existing code, data, databases and web applications.

Furthermore, you can easily extend the Template Toolkit through the use of its plugin mechanism and other developer APIs. Whatever context you use it in, the primary purpose of the Template Toolkit is to allow you to create a clear separation between the presentation elements of your web site and everything else. If you're generating static web pages, then you can use it to separate the commonly repeated user interface elements on each page headers, menus, footers, etc.

Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. The variable is assigned the output of the IF block which returns 'value' if true, but nothing if false. In other words, the following directive will always cause 'var' to be cleared.

They are called in the order defined, piping the output of one into the input of the next. Multiple directives can be included within a single tag when delimited by semi-colons. Note however that the TAGS directive must always be specified in a tag by itself. It's very nice. Click to hide the menu and go all widescreen!

Contents Top Close Open. Tag Styles Top Close Open. Outline Tags Top Close Open. The contents, possibly including any embedded template directives, are inserted intact. Absolute i. Both these options are disabled by default. For convenience, the filename does not need to be quoted as long as it contains only alphanumeric characters, underscores, dots or forward slashes.

Names containing any other characters should be quoted. All files should be unquoted names or quoted strings. Any variables should be interpolated into double-quoted strings. If a BLOCK of the specified name is defined in the same file, or in a file from which the current template has been called i. Any template directives embedded within the file will be processed accordingly.

All variables currently defined will be visible and accessible from within the included template. Local variable definitions may be specified after the template name, temporarily masking any existing variables.

Insignificant whitespace is ignored within directives so you can add variable definitions on the same line, the next line or split across several line with comments interspersed, if you prefer. Any changes made within the included template will not affect variables in the including template.

Technical Note: the localisation of the stash that is, the process by which variables are copied before an INCLUDE to prevent being overwritten is only skin deep. The top-level variable namespace hash is copied, but no attempt is made to perform a deep-copy of other structures hashes, arrays, objects, etc. Therefore, a foo variable referencing a hash will be copied to create a new foo variable but which points to the same hash array.

Thus, if you update compound variables e. If you're not worried about preserving variable values, or you trust the templates you're including then you might prefer to use the PROCESS directive which is faster by virtue of not performing any localisation.

However, be aware that because of the localisation issues explained above if you skipped the previous Technical Note above then you might want to go back and read it or skip this section too , the variables might not actually be "local". If the first element of the variable name already references a hash array then the variable update will affect the original variable.

This behaviour can be a little unpredictable and may well be improved upon in a future version. If you know what you're doing with it and you're sure that the variables in question are defined nor not as you expect them to be, then you can rely on this feature to implement some powerful "global" data sharing techniques. Otherwise, you might prefer to steer well clear and always pass simple undotted variables as parameters to INCLUDE and other similar directives.

The variable stash is localised once and then the templates specified are processed in order, all within that same variable context. This makes it slightly faster than specifying several separate INCLUDE directives because you only clone the variable stash once instead of n times , but not quite as "safe" because any variable changes in the first file will be visible in the second, third and so on. This might be what you want, of course, but then again, it might not.

Any changes made to variables within the included template will be visible in the including template. It's not unusual to find yourself adding common headers and footers to pages or sub-sections within a page. Something like this:. The individual template components being included might look like these:.

It encloses a block up to a matching END directive, which is first processed to generate some output. The specification order indicates outermost to innermost wrapper templates. For example, given the following template block definitions:.

A BLOCK definition can be used before it is defined, as long as the definition resides in the same file. The block definition itself does not generate any output. Like a named block, it can contain any other template directives which are processed when the block is defined.

The output generated by the block is then assigned to the variable julius. The enclosing block is processed each time the macro is called. Conditions may be arbitrarily complex and are evaluated with the same precedence as in Perl. Parenthesis may be used to explicitly determine evaluation order. Each CASE directive should contain a single value or a list of values which should match. When the FOREACH directive is used without specifying a target variable, any iterated values which are hash references will be automatically imported.

Note that this particular usage creates a localised variable context to prevent the imported hash keys from overwriting any existing variables. The imported definitions and any other variables defined in such a FOREACH loop will be lost at the end of the loop, when the previous context and variable values are restored.

However, under normal operation, the loop variable remains in scope after the FOREACH loop has ended caveat: overwriting any variable previously in scope. This is useful as the loop variable is secretly an iterator object see below and can be used to analyse the last entry processed by the loop. Each entry in the hash is returned in sorted order based on the key as a hash array containing 'key' and 'value' items. The LAST directive can be used to prematurely exit the loop. The following methods can be called on the loop iterator.

See Template::Iterator for further details. Nested loops will work as expected, with the loop variable correctly referencing the innermost loop and being restored to any previous value i. The iterator plugin can also be used to explicitly create an iterator object. This can be useful within nested loops where you need to keep a reference to the outer iterator within the inner loop. The iterator plugin effectively allows you to create an iterator by a name other than loop.



0コメント

  • 1000 / 1000