1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
<title>Template::Manual::Filters</title>
<link rel="stylesheet" type="text/css" href="../css/blue.css" title="Clear Blue">
<link rel="alternate stylesheet" type="text/css" href="../css/orange.css" title="Clear Orange">
<link rel="alternate stylesheet" type="text/css" href="../css/green.css" title="Clear Green">
<link rel="alternate stylesheet" type="text/css" href="../css/purple.css" title="Clear Purple">
<link rel="alternate stylesheet" type="text/css" href="../css/grey.css" title="Clear Grey">
<link rel="alternate stylesheet" type="text/css" href="../css/print.css" title="Print">
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="../css/ie6.css" />
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="../css/ie7.css" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="../css/print.css" media="print">
<script type="text/javascript" src="../js/tt2.js"></script>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="author" content="Andy Wardley">
</head>
<body id="body">
<div id="layout">
<div id="header">
<a href="../index.html" id="logo" alt="" title="Click for the Home Page"><span class="alt">TT2 Home Page</span></a>
<ul id="trail">
<li><a href="../manual/index.html">Manual</a></li>
<li class="last"><a href="../manual/Filters.html">Filters</a></li>
</ul>
<div class="controls">
<a href="#" class="menu show" onclick="widescreen_off(); return false" title="Show Menu">
<span class="about">Click to view the menu. It's very nice.</span>
</a>
<a href="#" class="menu hide" onclick="widescreen_on(); return false" title="Hide Menu">
<span class="about">Click to hide the menu and go all widescreen!</span>
</a>
<div class="pager">
<a href="../manual/Config.html" title="Template::Manual::Config" class="go back">Back<span class="about"><h4>Template::Manual::Config</h4>Configuration options</span></a>
<a href="../manual/index.html" title="Template::Manual" class="go up">Up<span class="about"><h4>Template::Manual</h4>Template Toolkit User Manual</span></a>
<a href="../manual/Plugins.html" title="Template::Manual::Plugins" class="go next">Next<span class="about"><h4>Template::Manual::Plugins</h4>Standard plugins</span></a>
</div>
</div>
<h1 class="headline">Template::Manual::Filters</h1>
<h2 class="subhead">Standard filters</h1>
</div>
<div id="page">
<div id="sidebar">
<a href="../index.html" id="logo"></a>
<div id="menu">
<ul class="menu">
<li class="l0 first"><a href="../manual/index.html" class="warm">Manual</a></li>
<li class="l1"><a href="../manual/Intro.html">Intro</a></li>
<li class="l1"><a href="../manual/Syntax.html">Syntax</a></li>
<li class="l1"><a href="../manual/Directives.html">Directives</a></li>
<li class="l1"><a href="../manual/Variables.html">Variables</a></li>
<li class="l1"><a href="../manual/VMethods.html">VMethods</a></li>
<li class="l1"><a href="../manual/Config.html">Config</a></li>
<li class="l1"><a href="../manual/Filters.html" class="warm">Filters</a></li>
<li class="l1"><a href="../manual/Plugins.html">Plugins</a></li>
<li class="l1"><a href="../manual/Internals.html">Internals</a></li>
<li class="l1"><a href="../manual/Views.html">Views</a></li>
<li class="l1"><a href="../manual/Credits.html">Credits</a></li>
<li class="l0"><a href="../modules/index.html">Modules</a></li>
<li class="l0"><a href="../tools/index.html">Tools</a></li>
<li class="l0"><a href="../tutorial/index.html">Tutorial</a></li>
<li class="l0 last"><a href="../faq/index.html">FAQ</a></li>
</ul>
<div class="foot"></div>
</div>
</div>
<div id="content">
<div class="section">
<div class="head">
<h1 id="contents" onclick="switch_section(this)" title="Click title to show/hide section content.">Contents</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<ul class="toc">
<li class=""><a href="#format_format_">format(format)</a></li>
<li class=""><a href="#upper">upper</a></li>
<li class=""><a href="#lower">lower</a></li>
<li class=""><a href="#ucfirst">ucfirst</a></li>
<li class=""><a href="#lcfirst">lcfirst</a></li>
<li class=""><a href="#trim">trim</a></li>
<li class=""><a href="#collapse">collapse</a></li>
<li class=""><a href="#html">html</a></li>
<li class=""><a href="#html_entity">html_entity</a></li>
<li class=""><a href="#xml">xml</a></li>
<li class=""><a href="#html_para">html_para</a></li>
<li class=""><a href="#html_break_html_para_break">html_break / html_para_break</a></li>
<li class=""><a href="#html_line_break">html_line_break</a></li>
<li class=""><a href="#uri">uri</a></li>
<li class=""><a href="#url">url</a></li>
<li class=""><a href="#indent_pad_">indent(pad)</a></li>
<li class=""><a href="#truncate_length_dots_">truncate(length,dots)</a></li>
<li class=""><a href="#repeat_iterations_">repeat(iterations)</a></li>
<li class=""><a href="#remove_string_">remove(string)</a></li>
<li class=""><a href="#replace_search_replace_">replace(search, replace)</a></li>
<li class=""><a href="#redirect_file_options_">redirect(file, options)</a></li>
<li class=""><a href="#eval_evaltt">eval / evaltt</a></li>
<li class=""><a href="#perl_evalperl">perl / evalperl</a></li>
<li class=""><a href="#stdout_options_">stdout(options)</a></li>
<li class=""><a href="#stderr">stderr</a></li>
<li class=""><a href="#null">null</a></li>
</ul>
</div>
</div>
<div class="pod">
<div class="section">
<div class="head">
<h1 id="format_format_" onclick="switch_section(this)" title="Click title to show/hide section content.">format(format)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>format</code> filter takes a format string as a parameter (as
per <code>printf()</code>) and formats each line of text accordingly.
</p>
<pre><span class="tt">[% FILTER format('<!-- %-40s -->') %]</span>
This is a block of text filtered
through the above format.
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre><!-- This is a block of text filtered -->
<!-- through the above format. --></pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="upper" onclick="switch_section(this)" title="Click title to show/hide section content.">upper</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Folds the input to UPPER CASE.
</p>
<pre><span class="tt">[% "hello world" FILTER upper %]</span></pre>
<p>
Output:
</p>
<pre>HELLO WORLD</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="lower" onclick="switch_section(this)" title="Click title to show/hide section content.">lower</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Folds the input to lower case.
</p>
<pre><span class="tt">[% "Hello World" FILTER lower %]</span></pre>
<p>
Output:
</p>
<pre>hello world</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="ucfirst" onclick="switch_section(this)" title="Click title to show/hide section content.">ucfirst</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Folds the first character of the input to UPPER CASE.
</p>
<pre><span class="tt">[% "hello" FILTER ucfirst %]</span></pre>
<p>
Output:
</p>
<pre>Hello</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="lcfirst" onclick="switch_section(this)" title="Click title to show/hide section content.">lcfirst</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Folds the first character of the input to lower case.
</p>
<pre><span class="tt">[% "HELLO" FILTER lcfirst %]</span></pre>
<p>
Output:
</p>
<pre>hELLO</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="trim" onclick="switch_section(this)" title="Click title to show/hide section content.">trim</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Trims any leading or trailing whitespace from the input text.
Particularly useful in conjunction with <code>INCLUDE</code>,
<code>PROCESS</code>, etc., having the same effect as the
<code>TRIM</code> configuration option.
</p>
<pre><span class="tt">[% INCLUDE myfile | trim %]</span></pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="collapse" onclick="switch_section(this)" title="Click title to show/hide section content.">collapse</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Collapse any whitespace sequences in the input text into a single space.
Leading and trailing whitespace (which would be reduced to a single
space) is removed, as per trim.
</p>
<pre><span class="tt">[% FILTER collapse %]</span>
The cat
sat on
the mat
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre>The cat sat on the mat</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="html" onclick="switch_section(this)" title="Click title to show/hide section content.">html</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Converts the characters <code><</code>, <code>></code>,
<code>&</code> and <code>"</code> to <code>&lt;</code>,
<code>&gt;</code>, <code>&amp;</code>, and
<code>&quot;</code> respectively, protecting them from being
interpreted as representing HTML tags or entities.
</p>
<pre><span class="tt">[% FILTER html %]</span>
Binary "<=>" returns -1, 0, or 1 depending on...
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre>Binary "&lt;=&gt;" returns -1, 0, or 1 depending on...</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="html_entity" onclick="switch_section(this)" title="Click title to show/hide section content.">html_entity</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>html</code> filter is fast and simple but it doesn't encode the
full range of HTML entities that your text may contain. The
<code>html_entity</code> filter uses either the <code>Apache::Util</code>
module (which is written in C and is therefore faster) or the
<code>HTML::Entities</code> module (written in Perl but equally as
comprehensive) to perform the encoding.
</p>
<p>
If one or other of these modules are installed on your system then the
text will be encoded (via the <code>escape_html()</code> or
<code>encode_entities()</code> subroutines respectively) to convert all
extended characters into their appropriate HTML entities (e.g. converting
'<code>é</code>' to '<code>&eacute;</code>'). If neither module is
available on your system then an '<code>html_entity</code>' exception
will be thrown reporting an appropriate message.
</p>
<p>
If you want to force TT to use one of the above modules in preference to
the other, then call either of the <a href="../modules/Template/Filters.html">Template::Filters</a> class methods:
<a href="../modules/Template/Filters.html#use_html_entities()">use_html_entities()</a> or <a href="../modules/Template/Filters.html#use_apache_util()">use_apache_util()</a>.
</p>
<pre>use Template::Filters;
Template::Filters->use_html_entities;</pre>
<p>
For further information on HTML entity encoding, see <a
href="http://www.w3.org/TR/REC-html40/sgml/entities.html">http://www.w3.org/TR/REC-html40/sgml/entities.html</a>.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="xml" onclick="switch_section(this)" title="Click title to show/hide section content.">xml</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Same as the <code>html</code> filter, but adds <code>&apos;</code>
which is the fifth XML built-in entity.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="html_para" onclick="switch_section(this)" title="Click title to show/hide section content.">html_para</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This filter formats a block of text into HTML paragraphs. A sequence of
two or more newlines is used as the delimiter for paragraphs which are
then wrapped in HTML <code><p></code>...<code></p></code>
tags.
</p>
<pre><span class="tt">[% FILTER html_para %]</span>
The cat sat on the mat.
Mary had a little lamb.
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre><p>
The cat sat on the mat.
</p>
<p>
Mary had a little lamb.
</p></pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="html_break_html_para_break" onclick="switch_section(this)" title="Click title to show/hide section content.">html_break / html_para_break</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Similar to the html_para filter described above, but uses the HTML tag
sequence <code><br><br></code> to join paragraphs.
</p>
<pre><span class="tt">[% FILTER html_break %]</span>
The cat sat on the mat.
Mary had a little lamb.
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre>The cat sat on the mat.
<br>
<br>
Mary had a little lamb.</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="html_line_break" onclick="switch_section(this)" title="Click title to show/hide section content.">html_line_break</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This filter replaces any newlines with <code><br></code> HTML tags,
thus preserving the line breaks of the original text in the HTML output.
</p>
<pre><span class="tt">[% FILTER html_line_break %]</span>
The cat sat on the mat.
Mary had a little lamb.
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre>The cat sat on the mat.<br>
Mary had a little lamb.<br></pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="uri" onclick="switch_section(this)" title="Click title to show/hide section content.">uri</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This filter URI escapes the input text, converting any characters outside
of the permitted URI character set (as defined by RFC 2396) into a
<code>%nn</code> hex escape.
</p>
<pre><span class="tt">[% 'my file.html' | uri %]</span></pre>
<p>
Output:
</p>
<pre>my%20file.html</pre>
<p>
The uri filter correctly encodes all reserved characters, including
<code>&</code>, <code>@</code>, <code>/</code>, <code>;</code>,
<code>:</code>, <code>=</code>, <code>+</code>, <code>?</code> and
<code>$</code>. This filter is typically used to encode parameters in a
URL that could otherwise be interpreted as part of the URL. Here's an
example:
</p>
<pre><span class="tt">[% path = 'http://tt2.org/example'
back = '/other?foo=bar&baz=bam'
title = 'Earth: "Mostly Harmless"'
%]</span>
<a href="<span class="tt">[% path %]</span>?back=<span class="tt">[% back | uri %]</span>&title=<span class="tt">[% title | uri %]</span>"></pre>
<p>
The output generated is rather long so we'll show it split across two
lines:
</p>
<pre><a href="http://tt2.org/example?back=%2Fother%3Ffoo%3Dbar%26
baz%3Dbam&title=Earth%3A%20%22Mostly%20Harmless%22"></pre>
<p>
Without the uri filter the output would look like this (also split across
two lines).
</p>
<pre><a href="http://tt2.org/example?back=/other?foo=bar
&baz=bam&title=Earth: "Mostly Harmless""></pre>
<p>
In this rather contrived example we've manage to generate both a broken
URL (the repeated <code>?</code> is not allowed) and a broken HTML
element (the href attribute is terminated by the first <code>"</code>
after <code>Earth: </code> leaving <code>Mostly Harmless"</code> dangling
on the end of the tag in precisely the way that harmless things shouldn't
dangle). So don't do that. Always use the uri filter to encode your URL
parameters.
</p>
<p>
However, you should <b>not</b> use the uri filter to encode an entire
URL.
</p>
<pre><a href="<span class="tt">[% page_url | uri %]</span>"> # WRONG!</pre>
<p>
This will incorrectly encode any reserved characters like <code>:</code>
and <code>/</code> and that's almost certainly not what you want in this
case. Instead you should use the <b>url</b> (note spelling) filter for
this purpose.
</p>
<pre><a href="<span class="tt">[% page_url | url %]</span>"> # CORRECT</pre>
<p>
Please note that this behaviour was changed in version 2.16 of the
Template Toolkit. Prior to that, the uri filter did not encode the
reserved characters, making it technically incorrect according to the RFC
2396 specification. So we fixed it in 2.16 and provided the url filter to
implement the old behaviour of not encoding reserved characters.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="url" onclick="switch_section(this)" title="Click title to show/hide section content.">url</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The url filter is a less aggressive version of the uri filter. It encodes
any characters outside of the permitted URI character set (as defined by
RFC 2396) into <code>%nn</code> hex escapes. However, unlike the uri
filter, the url filter does <b>not</b> encode the reserved characters
<code>&</code>, <code>@</code>, <code>/</code>, <code>;</code>,
<code>:</code>, <code>=</code>, <code>+</code>, <code>?</code> and
<code>$</code>.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="indent_pad_" onclick="switch_section(this)" title="Click title to show/hide section content.">indent(pad)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Indents the text block by a fixed pad string or width. The
'<code>pad</code>' argument can be specified as a string, or as a
numerical value to indicate a pad width (spaces). Defaults to 4 spaces if
unspecified.
</p>
<pre><span class="tt">[% FILTER indent('ME> ') %]</span>
blah blah blah
cabbages, rhubard, onions
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre>ME> blah blah blah
ME> cabbages, rhubard, onions</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="truncate_length_dots_" onclick="switch_section(this)" title="Click title to show/hide section content.">truncate(length,dots)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Truncates the text block to the length specified, or a default length of
32. Truncated text will be terminated with '<code>...</code>' (i.e. the
'<code>...</code>' falls inside the required length, rather than
appending to it).
</p>
<pre><span class="tt">[% FILTER truncate(21) %]</span>
I have much to say on this matter that has previously
been said on more than one occasion.
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre>I have much to say...</pre>
<p>
If you want to use something other than '<code>...</code>' you can pass
that as a second argument.
</p>
<pre><span class="tt">[% FILTER truncate(26, '&hellip;') %]</span>
I have much to say on this matter that has previously
been said on more than one occasion.
<span class="tt">[% END %]</span></pre>
<p>
Output:
</p>
<pre>I have much to say&hellip;</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="repeat_iterations_" onclick="switch_section(this)" title="Click title to show/hide section content.">repeat(iterations)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Repeats the text block for as many iterations as are specified (default:
1).
</p>
<pre><span class="tt">[% FILTER repeat(3) %]</span>
We want more beer and we want more beer,
<span class="tt">[% END %]</span>
We are the more beer wanters!</pre>
<p>
Output:
</p>
<pre>We want more beer and we want more beer,
We want more beer and we want more beer,
We want more beer and we want more beer,
We are the more beer wanters!</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="remove_string_" onclick="switch_section(this)" title="Click title to show/hide section content.">remove(string)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Searches the input text for any occurrences of the specified string and
removes them. A Perl regular expression may be specified as the search
string.
</p>
<pre><span class="tt">[% "The cat sat on the mat" FILTER remove('\s+') %]</span></pre>
<p>
Output:
</p>
<pre>Thecatsatonthemat</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="replace_search_replace_" onclick="switch_section(this)" title="Click title to show/hide section content.">replace(search, replace)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Similar to the remove filter described above, but taking a second
parameter which is used as a replacement string for instances of the
search string.
</p>
<pre><span class="tt">[% "The cat sat on the mat" | replace('\s+', '_') %]</span></pre>
<p>
Output:
</p>
<pre>The_cat_sat_on_the_mat</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="redirect_file_options_" onclick="switch_section(this)" title="Click title to show/hide section content.">redirect(file, options)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>redirect</code> filter redirects the output of the block into a
separate file, specified relative to the <code>OUTPUT_PATH</code>
configuration item.
</p>
<pre><span class="tt">[% FOREACH user IN myorg.userlist %]</span>
<span class="tt">[% FILTER redirect("users/${user.id}.html") %]</span>
<span class="tt">[% INCLUDE userinfo %]</span>
<span class="tt">[% END %]</span>
<span class="tt">[% END %]</span></pre>
<p>
or more succinctly, using side-effect notation:
</p>
<pre><span class="tt">[% FOREACH user IN myorg.userlist;
INCLUDE userinfo
FILTER redirect("users/${user.id}.html");
END
%]</span></pre>
<p>
A <code>file</code> exception will be thrown if the
<code>OUTPUT_PATH</code> option is undefined.
</p>
<p>
An optional <code>binmode</code> argument can follow the filename to
explicitly set the output file to binary mode.
</p>
<pre><span class="tt">[% PROCESS my/png/generator
FILTER redirect("images/logo.png", binmode=1) %]</span></pre>
<p>
For backwards compatibility with earlier versions, a single true/false
value can be used to set binary mode.
</p>
<pre><span class="tt">[% PROCESS my/png/generator
FILTER redirect("images/logo.png", 1) %]</span></pre>
<p>
For the sake of future compatibility and clarity, if nothing else, we
would strongly recommend you explicitly use the named
<code>binmode</code> option as shown in the first example.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="eval_evaltt" onclick="switch_section(this)" title="Click title to show/hide section content.">eval / evaltt</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>eval</code> filter evaluates the block as template text,
processing any directives embedded within it. This allows template
variables to contain template fragments, or for some method to be
provided for returning template fragments from an external source such as
a database, which can then be processed in the template as required.
</p>
<pre>my $vars = {
fragment => "The cat sat on the <span class="tt">[% place %]</span>",
};
$template->process($file, $vars);</pre>
<p>
The following example:
</p>
<pre><span class="tt">[% fragment | eval %]</span></pre>
<p>
is therefore equivalent to
</p>
<pre>The cat sat on the <span class="tt">[% place %]</span></pre>
<p>
The <code>evaltt</code> filter is provided as an alias for
<code>eval</code>.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="perl_evalperl" onclick="switch_section(this)" title="Click title to show/hide section content.">perl / evalperl</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>perl</code> filter evaluates the block as Perl code. The
<code>EVAL_PERL</code> option must be set to a true value or a
<code>perl</code> exception will be thrown.
</p>
<pre><span class="tt">[% my_perl_code | perl %]</span></pre>
<p>
In most cases, the <code>[% PERL %]</code> ... <code>[% END %]</code>
block should suffice for evaluating Perl code, given that template
directives are processed before being evaluate as Perl. Thus, the
previous example could have been written in the more verbose form:
</p>
<pre><span class="tt">[% PERL %]</span>
<span class="tt">[% my_perl_code %]</span>
<span class="tt">[% END %]</span></pre>
<p>
as well as
</p>
<pre><span class="tt">[% FILTER perl %]</span>
<span class="tt">[% my_perl_code %]</span>
<span class="tt">[% END %]</span></pre>
<p>
The <code>evalperl</code> filter is provided as an alias for
<code>perl</code> for backwards compatibility.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="stdout_options_" onclick="switch_section(this)" title="Click title to show/hide section content.">stdout(options)</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The stdout filter prints the output generated by the enclosing block to
<code>STDOUT</code>. The <code>binmode</code> option can be passed as
either a named parameter or a single argument to set <code>STDOUT</code>
to binary mode (see the binmode perl function).
</p>
<pre><span class="tt">[% PROCESS something/cool
FILTER stdout(binmode=1) # recommended %]</span>
<span class="tt">[% PROCESS something/cool
FILTER stdout(1) # alternate %]</span></pre>
<p>
The <code>stdout</code> filter can be used to force <code>binmode</code>
on <code>STDOUT</code>, or also inside <code>redirect</code>,
<code>null</code> or <code>stderr</code> blocks to make sure that
particular output goes to <code>STDOUT</code>. See the <code>null</code>
filter below for an example.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="stderr" onclick="switch_section(this)" title="Click title to show/hide section content.">stderr</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The stderr filter prints the output generated by the enclosing block to
<code>STDERR</code>.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="null" onclick="switch_section(this)" title="Click title to show/hide section content.">null</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>null</code> filter prints nothing. This is useful for plugins
whose methods return values that you don't want to appear in the output.
Rather than assigning every plugin method call to a dummy variable to
silence it, you can wrap the block in a null filter:
</p>
<pre><span class="tt">[% FILTER null;
USE im = GD.Image(100,100);
black = im.colorAllocate(0, 0, 0);
red = im.colorAllocate(255,0, 0);
blue = im.colorAllocate(0, 0, 255);
im.arc(50,50,95,75,0,360,blue);
im.fill(50,50,red);
im.png | stdout(1);
END;
-%]</span></pre>
<p>
Notice the use of the <code>stdout</code> filter to ensure that a
particular expression generates output to <code>STDOUT</code> (in this
case in binary mode).
</p>
</div>
</div>
</div></div>
<br class="clear" />
<div class="pageinfo">
<a href="http://template-toolkit.org/docs/manual/Filters.html">http://template-toolkit.org/docs/manual/Filters.html</a>
</div>
</div>
<div id="footer">
<a href="http://opensource.org/" class="osi"></a>
<div class="controls">
<div class="pager">
<a href="../manual/Config.html" title="Template::Manual::Config" class="go back">Back<span class="about"><h4>Template::Manual::Config</h4></span></a>
<a href="../manual/index.html" title="Template::Manual" class="go up">Up<span class="about"><h4>Template::Manual</h4></span></a>
<a href="../manual/Plugins.html" title="Template::Manual::Plugins" class="go next">Next<span class="about"><h4>Template::Manual::Plugins</h4></span></a>
</div>
</div>
<div class="copyright">
Copyright © 1996-2012 <a href="http://wardley.org/">Andy Wardley</a>. All Rights Reserved.
</div>
<div class="licence">
The <a href="http://template-toolkit.org/">Template Toolkit</a> is <a href="http://opensource.org/">Open Source</a> software.
You can redistribute and/or modify it under the terms of the <a href="http://www.opensource.org/licenses/gpl-license.php">GNU Public Licence</a>
or the <a href="http://www.opensource.org/licenses/artistic-license.php">Perl Artistic Licence</a>.
</div>
</div>
<div id="palette">
<ul>
<li class="first"><a href="#" class="blue" onclick="set_style('Clear Blue')"></a></li>
<li><a href="#" class="orange" onclick="set_style('Clear Orange')"></a></li>
<li><a href="#" class="green" onclick="set_style('Clear Green')"></a></li>
<li><a href="#" class="purple" onclick="set_style('Clear Purple')"></a></li>
<li><a href="#" class="grey" onclick="set_style('Clear Grey')"></a></li>
</ul>
</div>
</div> </body>
</html>
|