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 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
<title>Template::Context</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="../../modules/index.html">Modules</a></li>
<li><a href="../../modules/Template/index.html">Template::*</a></li>
<li class="last"><a href="../../modules/Template/Context.html">Context.pm</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="../../modules/Template/Constants.html" title="Template::Constants" class="go back">Back<span class="about"><h4>Template::Constants</h4>Defines constants for the Template Toolkit</span></a>
<a href="../../modules/Template/index.html" title="Template::* Modules" class="go up">Up<span class="about"><h4>Template::* Modules</h4></span></a>
<a href="../../modules/Template/Directive.html" title="Template::Directive" class="go next">Next<span class="about"><h4>Template::Directive</h4>Perl code generator for template directives</span></a>
</div>
</div>
<h1 class="headline">Template::Context</h1>
<h2 class="subhead">Runtime context in which templates are processed</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">Manual</a></li>
<li class="l0"><a href="../../modules/index.html" class="warm">Modules</a></li>
<li class="l1"><a href="../../modules/Template.html">Template.pm</a></li>
<li class="l1"><a href="../../modules/Template/index.html" class="warm">Template::*</a></li>
<li class="l2"><a href="../../modules/Template/Base.html">Base.pm</a></li>
<li class="l2"><a href="../../modules/Template/Config.html">Config.pm</a></li>
<li class="l2"><a href="../../modules/Template/Constants.html">Constants.pm</a></li>
<li class="l2"><a href="../../modules/Template/Context.html" class="warm">Context.pm</a></li>
<li class="l2"><a href="../../modules/Template/Directive.html">Directive.pm</a></li>
<li class="l2"><a href="../../modules/Template/Document.html">Document.pm</a></li>
<li class="l2"><a href="../../modules/Template/Exception.html">Exception.pm</a></li>
<li class="l2"><a href="../../modules/Template/Filters.html">Filters.pm</a></li>
<li class="l2"><a href="../../modules/Template/Grammar.html">Grammar.pm</a></li>
<li class="l2"><a href="../../modules/Template/Iterator.html">Iterator.pm</a></li>
<li class="l2"><a href="../../modules/Template/Namespace/index.html">Namespace::*</a></li>
<li class="l2"><a href="../../modules/Template/Parser.html">Parser.pm</a></li>
<li class="l2"><a href="../../modules/Template/Plugin.html">Plugin.pm</a></li>
<li class="l2"><a href="../../modules/Template/Plugin/index.html">Plugin::*</a></li>
<li class="l2"><a href="../../modules/Template/Plugins.html">Plugins.pm</a></li>
<li class="l2"><a href="../../modules/Template/Provider.html">Provider.pm</a></li>
<li class="l2"><a href="../../modules/Template/Service.html">Service.pm</a></li>
<li class="l2"><a href="../../modules/Template/Stash.html">Stash.pm</a></li>
<li class="l2"><a href="../../modules/Template/Stash/index.html">Stash::*</a></li>
<li class="l2"><a href="../../modules/Template/Test.html">Test.pm</a></li>
<li class="l2"><a href="../../modules/Template/VMethods.html">VMethods.pm</a></li>
<li class="l2"><a href="../../modules/Template/View.html">View.pm</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="#SYNOPSIS">SYNOPSIS</a></li>
<li class=""><a href="#DESCRIPTION">DESCRIPTION</a></li>
<li class=""><a href="#METHODS">METHODS</a></li>
<li class="sub"><a href="#method_new">new(\%params)</a></li>
<li class="subsub"><a href="#section_VARIABLES">VARIABLES</a></li>
<li class="subsub"><a href="#section_BLOCKS">BLOCKS</a></li>
<li class="subsub"><a href="#section_VIEWS">VIEWS</a></li>
<li class="subsub"><a href="#section_TRIM">TRIM</a></li>
<li class="subsub"><a href="#section_EVAL_PERL">EVAL_PERL</a></li>
<li class="subsub"><a href="#section_RECURSION">RECURSION</a></li>
<li class="subsub"><a href="#section_LOAD_TEMPLATES">LOAD_TEMPLATES</a></li>
<li class="subsub"><a href="#section_LOAD_PLUGINS">LOAD_PLUGINS</a></li>
<li class="subsub"><a href="#section_LOAD_FILTERS">LOAD_FILTERS</a></li>
<li class="subsub"><a href="#section_STASH">STASH</a></li>
<li class="subsub"><a href="#section_DEBUG">DEBUG</a></li>
<li class="sub"><a href="#method_template">template($name)</a></li>
<li class="sub"><a href="#method_plugin">plugin($name, \@args)</a></li>
<li class="sub"><a href="#method_filter">filter($name, \@args, $alias)</a></li>
<li class="sub"><a href="#method_process">process($template, \%vars)</a></li>
<li class="sub"><a href="#method_include">include($template, \%vars)</a></li>
<li class="sub"><a href="#method_insert">insert($template)</a></li>
<li class="sub"><a href="#method_throw">throw($error_type, $error_message, \$output)</a></li>
<li class="sub"><a href="#method_catch">catch($exception, \$output)</a></li>
<li class="sub"><a href="#method_define_block">define_block($name, $block)</a></li>
<li class="sub"><a href="#method_define_filter">define_filter($name, \&filter, $is_dynamic)</a></li>
<li class="sub"><a href="#method_define_vmethod">define_vmethod($type, $name, $code)</a></li>
<li class="sub"><a href="#method_define_view">define_view($name, \%params)</a></li>
<li class="sub"><a href="#method_define_views">define_views($views)</a></li>
<li class="sub"><a href="#method_stash">stash()</a></li>
<li class="sub"><a href="#method_localise">localise(\%vars)</a></li>
<li class="sub"><a href="#method_delocalise">delocalise()</a></li>
<li class="sub"><a href="#method_visit">visit(\%blocks)</a></li>
<li class="sub"><a href="#method_leave">leave()</a></li>
<li class="sub"><a href="#method_view">view()</a></li>
<li class="sub"><a href="#method_reset">reset()</a></li>
<li class="sub"><a href="#method_debugging">debugging($flag, @args)</a></li>
<li class="sub"><a href="#section_AUTOLOAD">AUTOLOAD</a></li>
<li class=""><a href="#AUTHOR">AUTHOR</a></li>
<li class=""><a href="#COPYRIGHT">COPYRIGHT</a></li>
<li class=""><a href="#SEE_ALSO">SEE ALSO</a></li>
</ul>
</div>
</div>
<div class="pod">
<div class="section">
<div class="head">
<h1 id="SYNOPSIS" onclick="switch_section(this)" title="Click title to show/hide section content.">SYNOPSIS</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<pre>use Template::Context;
# constructor
$context = Template::Context->new(\%config)
|| die $Template::Context::ERROR;
# fetch (load and compile) a template
$template = $context->template($template_name);
# fetch (load and instantiate) a plugin object
$plugin = $context->plugin($name, \@args);
# fetch (return or create) a filter subroutine
$filter = $context->filter($name, \@args, $alias);
# process/include a template, errors are thrown via die()
$output = $context->process($template, \%vars);
$output = $context->include($template, \%vars);
# raise an exception via die()
$context->throw($error_type, $error_message, \$output_buffer);
# catch an exception, clean it up and fix output buffer
$exception = $context->catch($exception, \$output_buffer);
# save/restore the stash to effect variable localisation
$new_stash = $context->localise(\%vars);
$old_stash = $context->delocalise();
# add new BLOCK or FILTER definitions
$context->define_block($name, $block);
$context->define_filter($name, \&filtersub, $is_dynamic);
# reset context, clearing any imported BLOCK definitions
$context->reset();
# methods for accessing internal items
$stash = $context->stash();
$tflag = $context->trim();
$epflag = $context->eval_perl();
$providers = $context->templates();
$providers = $context->plugins();
$providers = $context->filters();
...</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="DESCRIPTION" onclick="switch_section(this)" title="Click title to show/hide section content.">DESCRIPTION</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>Template::Context</code> module defines an object class for
representing a runtime context in which templates are processed. It
provides an interface to the fundamental operations of the Template
Toolkit processing engine through which compiled templates (i.e. Perl
code constructed from the template source) can process templates, load
plugins and filters, raise exceptions and so on.
</p>
<p>
A default <code>Template::Context</code> object is created by the <a
href="../../modules/Template.html">Template</a> module. Any
<code>Template::Context</code> options may be passed to the <a href="../../modules/Template.html">Template</a> <a href="../../modules/Template.html#method_new">new()</a> constructor method and
will be forwarded to the <code>Template::Context</code> constructor.
</p>
<pre>use Template;
my $template = Template->new({
TRIM => 1,
EVAL_PERL => 1,
BLOCKS => {
header => 'This is the header',
footer => 'This is the footer',
},
});</pre>
<p>
Similarly, the <code>Template::Context</code> constructor will forward
all configuration parameters onto other default objects (e.g. <a
href="../../modules/Template/Provider.html">Template::Provider</a>, <a
href="../../modules/Template/Plugins.html">Template::Plugins</a>, <a
href="../../modules/Template/Filters.html">Template::Filters</a>,
etc.) that it may need to instantiate.
</p>
<pre>$context = Template::Context->new({
INCLUDE_PATH => '/home/abw/templates', # provider option
TAG_STYLE => 'html', # parser option
});</pre>
<p>
A <code>Template::Context</code> object (or subclass) can be explicitly
instantiated and passed to the <a href="../../modules/Template.html">Template</a> <a href="../../modules/Template.html#method_new">new()</a> constructor method as the <code>CONTEXT</code>
configuration item.
</p>
<pre>use Template;
use Template::Context;
my $context = Template::Context->new({ TRIM => 1 });
my $template = Template->new({ CONTEXT => $context });</pre>
<p>
The <a href="../../modules/Template.html">Template</a> module uses the
<a href="../../modules/Template/Config.html">Template::Config</a> <a
href="../../modules/Template/Config.html#method_context">context()</a>
factory method to create a default context object when required. The
<code>$Template::Config::CONTEXT</code> package variable may be set to
specify an alternate context module. This will be loaded automatically
and its <a href="#method_new">new()</a> constructor method called by the
<a href="../../modules/Template/Config.html#method_context">context()</a> factory method when a default context
object is required.
</p>
<pre>use Template;
$Template::Config::CONTEXT = 'MyOrg::Template::Context';
my $template = Template->new({
EVAL_PERL => 1,
EXTRA_MAGIC => 'red hot', # your extra config items
...
});</pre>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="METHODS" onclick="switch_section(this)" title="Click title to show/hide section content.">METHODS</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<div class="subsection">
<div class="head">
<h2 id="method_new" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">new(\%params)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
The <code>new()</code> constructor method is called to instantiate a
<code>Template::Context</code> object. Configuration parameters may be
specified as a HASH reference or as a list of <code>name =>
value</code> pairs.
</p>
<pre>my $context = Template::Context->new({
INCLUDE_PATH => 'header',
POST_PROCESS => 'footer',
});
my $context = Template::Context->new( EVAL_PERL => 1 );</pre>
<p>
The <code>new()</code> method returns a <code>Template::Context</code>
object or <code>undef</code> on error. In the latter case, a relevant
error message can be retrieved by the <a href="../../modules/Template/Base.html#method_error">error()</a> class method or
directly from the <code>$Template::Context::ERROR</code> package
variable.
</p>
<pre>my $context = Template::Context->new(\%config)
|| die Template::Context->error();
my $context = Template::Context->new(\%config)
|| die $Template::Context::ERROR;</pre>
<p>
The following configuration items may be specified. Please see <a
href="../../manual/Config.html">Template::Manual::Config</a> for further details.
</p>
<h3 class="method" id="section_VARIABLES">VARIABLES</h3>
<p>
The <a href="../../manual/Config.html#section_VARIABLES">VARIABLES</a> option can be used to specify a hash
array of template variables.
</p>
<pre>my $context = Template::Context->new({
VARIABLES => {
title => 'A Demo Page',
author => 'Joe Random Hacker',
version => 3.14,
},
};</pre>
<h3 class="method" id="section_BLOCKS">BLOCKS</h3>
<p>
The <a href="../../manual/Config.html#section_BLOCKS">BLOCKS</a> option can be used to pre-define a default
set of template blocks.
</p>
<pre>my $context = Template::Context->new({
BLOCKS => {
header => 'The Header. <span class="tt">[% title %]</span>',
footer => sub { return $some_output_text },
another => Template::Document->new({ ... }),
},
});</pre>
<h3 class="method" id="section_VIEWS">VIEWS</h3>
<p>
The <a href="../../manual/Config.html#section_VIEWS">VIEWS</a> option can be used to pre-define one or more
<a href="../../modules/Template/View.html">Template::View</a> objects.
</p>
<pre>my $context = Template::Context->new({
VIEWS => [
bottom => { prefix => 'bottom/' },
middle => { prefix => 'middle/', base => 'bottom' },
top => { prefix => 'top/', base => 'middle' },
],
});</pre>
<h3 class="method" id="section_TRIM">TRIM</h3>
<p>
The <a href="../../manual/Config.html#section_TRIM">TRIM</a> option can be set to have any leading and
trailing whitespace automatically removed from the output of all template
files and <code>BLOCK</code>s.
</p>
<p>
example:
</p>
<pre><span class="tt">[% BLOCK foo %]</span>
Line 1 of foo
<span class="tt">[% END %]</span>
before
<span class="tt">[% INCLUDE foo %]</span>
after</pre>
<p>
output:
</p>
<pre>before
Line 1 of foo
after</pre>
<h3 class="method" id="section_EVAL_PERL">EVAL_PERL</h3>
<p>
The <a href="../../manual/Config.html#section_EVAL_PERL">EVAL_PERL</a> is used to indicate if
<code>PERL</code> and/or <code>RAWPERL</code> blocks should be evaluated.
It is disabled by default.
</p>
<h3 class="method" id="section_RECURSION">RECURSION</h3>
<p>
The <a href="../../manual/Config.html#section_RECURSION">RECURSION</a> can be set to allow templates to
recursively process themselves, either directly (e.g. template
<code>foo</code> calls <code>INCLUDE foo</code>) or indirectly (e.g.
<code>foo</code> calls <code>INCLUDE bar</code> which calls <code>INCLUDE
foo</code>).
</p>
<h3 class="method" id="section_LOAD_TEMPLATES">LOAD_TEMPLATES</h3>
<p>
The <a href="../../manual/Config.html#section_LOAD_TEMPLATES">LOAD_TEMPLATES</a> option can be used to
provide a reference to a list of <a href="../../modules/Template/Provider.html">Template::Provider</a> objects or
sub-classes thereof which will take responsibility for loading and
compiling templates.
</p>
<pre>my $context = Template::Context->new({
LOAD_TEMPLATES => [
MyOrg::Template::Provider->new({ ... }),
Template::Provider->new({ ... }),
],
});</pre>
<h3 class="method" id="section_LOAD_PLUGINS">LOAD_PLUGINS</h3>
<p>
The <a href="../../manual/Config.html#section_LOAD_PLUGINS">LOAD_PLUGINS</a> options can be used to specify
a list of provider objects responsible for loading and instantiating
template plugin objects.
</p>
<pre>my $context = Template::Context->new({
LOAD_PLUGINS => [
MyOrg::Template::Plugins->new({ ... }),
Template::Plugins->new({ ... }),
],
});</pre>
<h3 class="method" id="section_LOAD_FILTERS">LOAD_FILTERS</h3>
<p>
The <a href="../../manual/Config.html#section_LOAD_FILTERS">LOAD_FILTERS</a> option can be used to specify
a list of provider objects for returning and/or creating filter
subroutines.
</p>
<pre>my $context = Template::Context->new({
LOAD_FILTERS => [
MyTemplate::Filters->new(),
Template::Filters->new(),
],
});</pre>
<h3 class="method" id="section_STASH">STASH</h3>
<p>
The <a href="../../manual/Config.html#section_STASH">STASH</a> option can be used to specify a <a href="../../modules/Template/Stash.html">Template::Stash</a> object or sub-class
which will take responsibility for managing template variables.
</p>
<pre>my $stash = MyOrg::Template::Stash->new({ ... });
my $context = Template::Context->new({
STASH => $stash,
});</pre>
<h3 class="method" id="section_DEBUG">DEBUG</h3>
<p>
The <a href="../../manual/Config.html#section_DEBUG">DEBUG</a> option can be used to enable various
debugging features of the <a href="../../modules/Template/Context.html">Template::Context</a> module.
</p>
<pre>use Template::Constants qw( :debug );
my $template = Template->new({
DEBUG => DEBUG_CONTEXT | DEBUG_DIRS,
});</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_template" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">template($name)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Returns a compiled template by querying each of the <a
href="#section_LOAD_TEMPLATES">LOAD_TEMPLATES</a> providers (instances of
<a href="../../modules/Template/Provider.html">Template::Provider</a>,
or sub-class) in turn.
</p>
<pre>$template = $context->template('header');</pre>
<p>
On error, a <a href="../../modules/Template/Exception.html">Template::Exception</a> object of type '<code>file</code>' is thrown
via <code>die()</code>. This can be caught by enclosing the call to
<code>template()</code> in an <code>eval</code> block and examining
<code>$@</code>.
</p>
<pre>eval { $template = $context->template('header') };
if ($@) {
print "failed to fetch template: $@\n";
}</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_plugin" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">plugin($name, \@args)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Instantiates a plugin object by querying each of the <a
href="#section_LOAD_PLUGINS">LOAD_PLUGINS</a> providers. The default <a
href="#section_LOAD_PLUGINS">LOAD_PLUGINS</a> provider is a <a href="../../modules/Template/Plugins.html">Template::Plugins</a> object which
attempts to load plugin modules, according the various configuration
items such as <a href="../../modules/Template/Plugins.html#section_PLUGIN_BASE">PLUGIN_BASE</a>, <a href="../../modules/Template/Plugins.html#section_LOAD_PERL">LOAD_PERL</a>,
etc., and then instantiate an object via <a href="../../modules/Template/Plugin.html#method_new">new()</a>. A reference to a
list of constructor arguments may be passed as the second parameter.
These are forwarded to the plugin constructor.
</p>
<p>
Returns a reference to a plugin (which is generally an object, but
doesn't have to be). Errors are thrown as <a href="../../modules/Template/Exception.html">Template::Exception</a> objects
with the type set to '<code>plugin</code>'.
</p>
<pre>$plugin = $context->plugin('DBI', 'dbi:msql:mydbname');</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_filter" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">filter($name, \@args, $alias)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Instantiates a filter subroutine by querying the <a
href="#section_LOAD_FILTERS">LOAD_FILTERS</a> providers. The default <a
href="#section_LOAD_FILTERS">LOAD_FILTERS</a> provider is a <a href="../../modules/Template/Filters.html">Template::Filters</a> object.
</p>
<p>
Additional arguments may be passed by list reference along with an
optional alias under which the filter will be cached for subsequent use.
The filter is cached under its own <code>$name</code> if
<code>$alias</code> is undefined. Subsequent calls to
<code>filter($name)</code> will return the cached entry, if defined.
Specifying arguments bypasses the caching mechanism and always creates a
new filter. Errors are thrown as <a href="../../modules/Template/Exception.html">Template::Exception</a> objects
with the type set to '<code>filter</code>'.
</p>
<pre># static filter (no args)
$filter = $context->filter('html');
# dynamic filter (args) aliased to 'padright'
$filter = $context->filter('format', '%60s', 'padright');
# retrieve previous filter via 'padright' alias
$filter = $context->filter('padright');</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_process" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">process($template, \%vars)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Processes a template named or referenced by the first parameter and
returns the output generated. An optional reference to a hash array may
be passed as the second parameter, containing variable definitions which
will be set before the template is processed. The template is processed
in the current context, with no localisation of variables performed.
Errors are thrown as <a href="../../modules/Template/Exception.html">Template::Exception</a> objects via <code>die()</code>.
</p>
<pre>$output = $context->process('header', { title => 'Hello World' });</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_include" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">include($template, \%vars)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Similar to <a href="#method_process">process()</a>, but using localised
variables. Changes made to any variables will only persist until the
<code>include()</code> method completes.
</p>
<pre>$output = $context->include('header', { title => 'Hello World' });</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_insert" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">insert($template)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method returns the source content of a template file without
performing any evaluation. It is used to implement the
<code>INSERT</code> directive.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_throw" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">throw($error_type, $error_message, \$output)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Raises an exception in the form of a <a href="../../modules/Template/Exception.html">Template::Exception</a> object by
calling <code>die()</code>. This method may be passed a reference to an
existing <a href="../../modules/Template/Exception.html">Template::Exception</a> object; a single value containing an error
message which is used to instantiate a <a href="../../modules/Template/Exception.html">Template::Exception</a> of type
'<code>undef</code>'; or a pair of values representing the exception
<code>type</code> and <code>info</code> from which a <a href="../../modules/Template/Exception.html">Template::Exception</a> object is
instantiated. e.g.
</p>
<pre>$context->throw($exception);
$context->throw("I'm sorry Dave, I can't do that");
$context->throw('denied', "I'm sorry Dave, I can't do that");</pre>
<p>
The optional third parameter may be a reference to the current output
buffer. This is then stored in the exception object when created,
allowing the catcher to examine and use the output up to the point at
which the exception was raised.
</p>
<pre>$output .= 'blah blah blah';
$output .= 'more rhubarb';
$context->throw('yack', 'Too much yacking', \$output);</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_catch" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">catch($exception, \$output)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Catches an exception thrown, either as a reference to a <a href="../../modules/Template/Exception.html">Template::Exception</a> object or
some other value. In the latter case, the error string is promoted to a
<a href="../../modules/Template/Exception.html">Template::Exception</a> object of '<code>undef</code>' type. This
method also accepts a reference to the current output buffer which is
passed to the <a href="../../modules/Template/Exception.html">Template::Exception</a> constructor, or is appended to the output
buffer stored in an existing <a href="../../modules/Template/Exception.html">Template::Exception</a> object, if
unique (i.e. not the same reference). By this process, the correct state
of the output buffer can be reconstructed for simple or nested throws.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_define_block" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">define_block($name, $block)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Adds a new block definition to the internal <a
href="#section_BLOCKS">BLOCKS</a> cache. The first argument should
contain the name of the block and the second a reference to a <a
href="../../modules/Template/Document.html">Template::Document</a>
object or template sub-routine, or template text which is automatically
compiled into a template sub-routine.
</p>
<p>
Returns a true value (the sub-routine or <a href="../../modules/Template/Document.html">Template::Document</a> reference) on
success or undef on failure. The relevant error message can be retrieved
by calling the <a href="../../modules/Template/Base.html#method_error">error()</a> method.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_define_filter" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">define_filter($name, \&filter, $is_dynamic)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Adds a new filter definition by calling the <a href="../../modules/Template/Filters.html#method_store">store()</a> method on
each of the <a href="#section_LOAD_FILTERS">LOAD_FILTERS</a> providers
until accepted (in the usual case, this is accepted straight away by the
one and only <a href="../../modules/Template/Filters.html">Template::Filters</a> provider). The first argument should contain
the name of the filter and the second a reference to a filter subroutine.
The optional third argument can be set to any true value to indicate that
the subroutine is a dynamic filter factory.
</p>
<p>
Returns a true value or throws a '<code>filter</code>' exception on
error.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_define_vmethod" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">define_vmethod($type, $name, $code)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method is a wrapper around the <a href="../../modules/Template/Stash.html">Template::Stash</a> <a href="../../modules/Template/Stash.html#method_define_vmethod">define_vmethod()</a> method. It can be used to
define new virtual methods.
</p>
<pre># define a new scalar (item) virtual method
$context->define_vmethod(
item => ucfirst => sub {
my $text = shift;
return ucfirst $text;
}
)</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_define_view" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">define_view($name, \%params)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method allows you to define a named <a href="../../modules/Template/View.html">view</a>.
</p>
<pre>$context->define_view(
my_view => {
prefix => 'my_templates/'
}
);</pre>
<p>
The view is then accessible as a template variable.
</p>
<pre><span class="tt">[% my_view.print(some_data) %]</span></pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_define_views" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">define_views($views)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method allows you to define multiple named <a href="../../modules/Template/View.html">views</a>. A reference to a hash array
or list reference should be passed as an argument.
</p>
<pre>$context->define_view({ # hash reference
my_view_one => {
prefix => 'my_templates_one/'
},
my_view_two => {
prefix => 'my_templates_two/'
}
});</pre>
<p>
If you're defining multiple views of which one or more are based on other
views in the same definition then you should pass them as a list
reference. This ensures that they get created in the right order (Perl
does not preserve the order of items defined in a hash reference so you
can't guarantee that your base class view will be defined before your
subclass view).
</p>
<pre>$context->define_view([ # list referenence
my_view_one => {
prefix => 'my_templates_one/'
},
my_view_two => {
prefix => 'my_templates_two/' ,
base => 'my_view_one',
}
]);</pre>
<p>
The views are then accessible as template variables.
</p>
<pre><span class="tt">[% my_view_one.print(some_data) %]</span>
<span class="tt">[% my_view_two.print(some_data) %]</span></pre>
<p>
See also the <a href="#section_VIEWS">VIEWS</a> option.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_stash" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">stash()</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method returns the <a href="../../modules/Template/Stash.html">Template::Stash</a> object used internally to manage template
variables.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_localise" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">localise(\%vars)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Clones the stash to create a context with localised variables. Returns a
reference to the newly cloned stash object which is also stored
internally.
</p>
<pre>$stash = $context->localise();</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_delocalise" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">delocalise()</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Restore the stash to its state prior to localisation.
</p>
<pre>$stash = $context->delocalise();</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_visit" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">visit(\%blocks)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method is called by <a href="../../modules/Template/Document.html">Template::Document</a> objects immediately before they process their
content. It is called to register any local <code>BLOCK</code>
definitions with the context object so that they may be subsequently
delivered on request.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_leave" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">leave()</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Compliment to the <a href="#method_visit">visit()</a> method. Called by
<a href="../../modules/Template/Document.html">Template::Document</a>
objects immediately after they process their content.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_view" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">view()</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method creates a <a href="../../modules/Template/View.html">Template::View</a> object bound to the context.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_reset" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">reset()</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Clears the local <a href="#section_BLOCKS">BLOCKS</a> cache of any
<code>BLOCK</code> definitions. Any initial set of <a
href="#section_BLOCKS">BLOCKS</a> specified as a configuration item to
the constructor will be reinstated.
</p>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="method_debugging" class="method" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">debugging($flag, @args)</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
This method is used to control debugging output. It is used to implement
the <a href="../../manual/Directives.html#section_DEBUG">DEBUG</a> directive.
</p>
<p>
The first argument can be <code>on</code> or <code>off</code> to enable
or disable debugging respectively. The numerical values <code>0</code>
and <code>1</code> can also be used if you prefer.
</p>
<pre>$context->debugging('on');</pre>
<p>
Alternately, the first argument can be <code>format</code> to define a
new debug message format. The second argument should be the format string
which can contain any of the <code>$file</code>, <code>$line</code> or
<code>$text</code> symbols to indicate where the relevant values should
be inserted.
</p>
<pre># note single quotes to prevent interpolated of variables
$context->debugging( format => '## $file line $line: $text' );</pre>
<p>
The final use of this method is to generate debugging messages
themselves. The first argument should be <code>msg</code>, followed by a
reference to a hash array of value to insert into the debugging format
string.
</p>
<pre>$context->debugging(
msg => {
line => 20,
file => 'example.tt',
text => 'Trampoline! Trampoline!',
}
);</pre>
</div>
</div> <div class="subsection">
<div class="head">
<h2 id="section_AUTOLOAD" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">AUTOLOAD</h2>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
An <code>AUTOLOAD</code> method provides access to context configuration
items.
</p>
<pre>$stash = $context->stash();
$tflag = $context->trim();
$epflag = $context->eval_perl();
...</pre>
</div>
</div>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="AUTHOR" onclick="switch_section(this)" title="Click title to show/hide section content.">AUTHOR</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Andy Wardley <abw@wardley.org> <a
href="http://wardley.org/">http://wardley.org/</a>
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="COPYRIGHT" onclick="switch_section(this)" title="Click title to show/hide section content.">COPYRIGHT</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
Copyright (C) 1996-2012 Andy Wardley. All Rights Reserved.
</p>
<p>
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
</p>
</div>
</div>
<div class="section">
<div class="head">
<h1 id="SEE_ALSO" onclick="switch_section(this)" title="Click title to show/hide section content.">SEE ALSO</h1>
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
</div>
<div class="body">
<p>
<a href="../../modules/Template.html">Template</a>, <a href="../../modules/Template/Document.html">Template::Document</a>, <a href="../../modules/Template/Exception.html">Template::Exception</a>, <a
href="../../modules/Template/Filters.html">Template::Filters</a>, <a
href="../../modules/Template/Plugins.html">Template::Plugins</a>, <a
href="../../modules/Template/Provider.html">Template::Provider</a>, <a
href="../../modules/Template/Service.html">Template::Service</a>, <a
href="../../modules/Template/Stash.html">Template::Stash</a>
</p>
</div>
</div>
</div></div>
<br class="clear" />
<div class="pageinfo">
<a href="http://template-toolkit.org/docs/modules/Template/Context.html">http://template-toolkit.org/docs/modules/Template/Context.html</a>
</div>
</div>
<div id="footer">
<a href="http://opensource.org/" class="osi"></a>
<div class="controls">
<div class="pager">
<a href="../../modules/Template/Constants.html" title="Template::Constants" class="go back">Back<span class="about"><h4>Template::Constants</h4></span></a>
<a href="../../modules/Template/index.html" title="Template::* Modules" class="go up">Up<span class="about"><h4>Template::* Modules</h4></span></a>
<a href="../../modules/Template/Directive.html" title="Template::Directive" class="go next">Next<span class="about"><h4>Template::Directive</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>
|