{
    "content": [
        {
            "type": "text",
            "text": "# Template::Context (perldoc)\n\n## NAME\n\nTemplate::Context - Runtime context in which templates are processed\n\n## SYNOPSIS\n\nuse Template::Context;\n# constructor\n$context = Template::Context->new(\\%config)\n|| die $Template::Context::ERROR;\n# fetch (load and compile) a template\n$template = $context->template($templatename);\n# fetch (load and instantiate) a plugin object\n$plugin = $context->plugin($name, \\@args);\n# fetch (return or create) a filter subroutine\n$filter = $context->filter($name, \\@args, $alias);\n# process/include a template, errors are thrown via die()\n$output = $context->process($template, \\%vars);\n$output = $context->include($template, \\%vars);\n# raise an exception via die()\n$context->throw($errortype, $errormessage, \\$outputbuffer);\n# catch an exception, clean it up and fix output buffer\n$exception = $context->catch($exception, \\$outputbuffer);\n# save/restore the stash to effect variable localisation\n$newstash = $context->localise(\\%vars);\n$oldstash = $context->delocalise();\n# add new BLOCK or FILTER definitions\n$context->defineblock($name, $block);\n$context->definefilter($name, \\&filtersub, $isdynamic);\n# reset context, clearing any imported BLOCK definitions\n$context->reset();\n# methods for accessing internal items\n$stash     = $context->stash();\n$tflag     = $context->trim();\n$epflag    = $context->evalperl();\n$providers = $context->templates();\n$providers = $context->plugins();\n$providers = $context->filters();\n...\n\n## DESCRIPTION\n\nThe \"Template::Context\" module defines an object class for representing a runtime context in\nwhich templates are processed. It provides an interface to the fundamental operations of the\nTemplate Toolkit processing engine through which compiled templates (i.e. Perl code constructed\nfrom the template source) can process templates, load plugins and filters, raise exceptions and\nso on.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **METHODS**\n- **AUTHOR**\n- **COPYRIGHT**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Template::Context",
        "section": "",
        "mode": "perldoc",
        "summary": "Template::Context - Runtime context in which templates are processed",
        "synopsis": "use Template::Context;\n# constructor\n$context = Template::Context->new(\\%config)\n|| die $Template::Context::ERROR;\n# fetch (load and compile) a template\n$template = $context->template($templatename);\n# fetch (load and instantiate) a plugin object\n$plugin = $context->plugin($name, \\@args);\n# fetch (return or create) a filter subroutine\n$filter = $context->filter($name, \\@args, $alias);\n# process/include a template, errors are thrown via die()\n$output = $context->process($template, \\%vars);\n$output = $context->include($template, \\%vars);\n# raise an exception via die()\n$context->throw($errortype, $errormessage, \\$outputbuffer);\n# catch an exception, clean it up and fix output buffer\n$exception = $context->catch($exception, \\$outputbuffer);\n# save/restore the stash to effect variable localisation\n$newstash = $context->localise(\\%vars);\n$oldstash = $context->delocalise();\n# add new BLOCK or FILTER definitions\n$context->defineblock($name, $block);\n$context->definefilter($name, \\&filtersub, $isdynamic);\n# reset context, clearing any imported BLOCK definitions\n$context->reset();\n# methods for accessing internal items\n$stash     = $context->stash();\n$tflag     = $context->trim();\n$epflag    = $context->evalperl();\n$providers = $context->templates();\n$providers = $context->plugins();\n$providers = $context->filters();\n...",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 45,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 54,
                "subsections": []
            },
            {
                "name": "METHODS",
                "lines": 375,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 3,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Template::Context - Runtime context in which templates are processed\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "use Template::Context;\n\n# constructor\n$context = Template::Context->new(\\%config)\n|| die $Template::Context::ERROR;\n\n# fetch (load and compile) a template\n$template = $context->template($templatename);\n\n# fetch (load and instantiate) a plugin object\n$plugin = $context->plugin($name, \\@args);\n\n# fetch (return or create) a filter subroutine\n$filter = $context->filter($name, \\@args, $alias);\n\n# process/include a template, errors are thrown via die()\n$output = $context->process($template, \\%vars);\n$output = $context->include($template, \\%vars);\n\n# raise an exception via die()\n$context->throw($errortype, $errormessage, \\$outputbuffer);\n\n# catch an exception, clean it up and fix output buffer\n$exception = $context->catch($exception, \\$outputbuffer);\n\n# save/restore the stash to effect variable localisation\n$newstash = $context->localise(\\%vars);\n$oldstash = $context->delocalise();\n\n# add new BLOCK or FILTER definitions\n$context->defineblock($name, $block);\n$context->definefilter($name, \\&filtersub, $isdynamic);\n\n# reset context, clearing any imported BLOCK definitions\n$context->reset();\n\n# methods for accessing internal items\n$stash     = $context->stash();\n$tflag     = $context->trim();\n$epflag    = $context->evalperl();\n$providers = $context->templates();\n$providers = $context->plugins();\n$providers = $context->filters();\n...\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "The \"Template::Context\" module defines an object class for representing a runtime context in\nwhich templates are processed. It provides an interface to the fundamental operations of the\nTemplate Toolkit processing engine through which compiled templates (i.e. Perl code constructed\nfrom the template source) can process templates, load plugins and filters, raise exceptions and\nso on.\n\nA default \"Template::Context\" object is created by the Template module. Any \"Template::Context\"\noptions may be passed to the Template new() constructor method and will be forwarded to the\n\"Template::Context\" constructor.\n\nuse Template;\n\nmy $template = Template->new({\nTRIM      => 1,\nEVALPERL => 1,\nBLOCKS    => {\nheader => 'This is the header',\nfooter => 'This is the footer',\n},\n});\n\nSimilarly, the \"Template::Context\" constructor will forward all configuration parameters onto\nother default objects (e.g. Template::Provider, Template::Plugins, Template::Filters, etc.) that\nit may need to instantiate.\n\n$context = Template::Context->new({\nINCLUDEPATH => '/home/abw/templates', # provider option\nTAGSTYLE    => 'html',                # parser option\n});\n\nA \"Template::Context\" object (or subclass) can be explicitly instantiated and passed to the\nTemplate new() constructor method as the \"CONTEXT\" configuration item.\n\nuse Template;\nuse Template::Context;\n\nmy $context  = Template::Context->new({ TRIM => 1 });\nmy $template = Template->new({ CONTEXT => $context });\n\nThe Template module uses the Template::Config context() factory method to create a default\ncontext object when required. The $Template::Config::CONTEXT package variable may be set to\nspecify an alternate context module. This will be loaded automatically and its new() constructor\nmethod called by the context() factory method when a default context object is required.\n\nuse Template;\n\n$Template::Config::CONTEXT = 'MyOrg::Template::Context';\n\nmy $template = Template->new({\nEVALPERL   => 1,\nEXTRAMAGIC => 'red hot',  # your extra config items\n...\n});\n",
                "subsections": []
            },
            "METHODS": {
                "content": "new(\\%params)\nThe \"new()\" constructor method is called to instantiate a \"Template::Context\" object.\nConfiguration parameters may be specified as a HASH reference or as a list of \"name => value\"\npairs.\n\nmy $context = Template::Context->new({\nINCLUDEPATH => 'header',\nPOSTPROCESS => 'footer',\n});\n\nmy $context = Template::Context->new( EVALPERL => 1 );\n\nThe \"new()\" method returns a \"Template::Context\" object or \"undef\" on error. In the latter case,\na relevant error message can be retrieved by the error() class method or directly from the\n$Template::Context::ERROR package variable.\n\nmy $context = Template::Context->new(\\%config)\n|| die Template::Context->error();\n\nmy $context = Template::Context->new(\\%config)\n|| die $Template::Context::ERROR;\n\nThe following configuration items may be specified. Please see Template::Manual::Config for\nfurther details.\n\nVARIABLES\nThe VARIABLES option can be used to specify a hash array of template variables.\n\nmy $context = Template::Context->new({\nVARIABLES => {\ntitle   => 'A Demo Page',\nauthor  => 'Joe Random Hacker',\nversion => 3.14,\n},\n};\n\nBLOCKS\nThe BLOCKS option can be used to pre-define a default set of template blocks.\n\nmy $context = Template::Context->new({\nBLOCKS => {\nheader  => 'The Header.  [% title %]',\nfooter  => sub { return $someoutputtext },\nanother => Template::Document->new({ ... }),\n},\n});\n\nVIEWS\nThe VIEWS option can be used to pre-define one or more Template::View objects.\n\nmy $context = Template::Context->new({\nVIEWS => [\nbottom => { prefix => 'bottom/' },\nmiddle => { prefix => 'middle/', base => 'bottom' },\ntop    => { prefix => 'top/',    base => 'middle' },\n],\n});\n\nTRIM\nThe TRIM option can be set to have any leading and trailing whitespace automatically removed\nfrom the output of all template files and \"BLOCK\"s.\n\nexample:\n\n[% BLOCK foo %]\n\nLine 1 of foo\n\n[% END %]\n\nbefore\n[% INCLUDE foo %]\nafter\n\noutput:\n\nbefore\nLine 1 of foo\nafter\n\nEVALPERL\nThe EVALPERL is used to indicate if \"PERL\" and/or \"RAWPERL\" blocks should be evaluated. It is\ndisabled by default.\n\nRECURSION\nThe RECURSION can be set to allow templates to recursively process themselves, either directly\n(e.g. template \"foo\" calls \"INCLUDE foo\") or indirectly (e.g. \"foo\" calls \"INCLUDE bar\" which\ncalls \"INCLUDE foo\").\n\nLOADTEMPLATES\nThe LOADTEMPLATES option can be used to provide a reference to a list of Template::Provider\nobjects or sub-classes thereof which will take responsibility for loading and compiling\ntemplates.\n\nmy $context = Template::Context->new({\nLOADTEMPLATES => [\nMyOrg::Template::Provider->new({ ... }),\nTemplate::Provider->new({ ... }),\n],\n});\n\nLOADPLUGINS\nThe LOADPLUGINS options can be used to specify a list of provider objects responsible for\nloading and instantiating template plugin objects.\n\nmy $context = Template::Context->new({\nLOADPLUGINS => [\nMyOrg::Template::Plugins->new({ ... }),\nTemplate::Plugins->new({ ... }),\n],\n});\n\nLOADFILTERS\nThe LOADFILTERS option can be used to specify a list of provider objects for returning and/or\ncreating filter subroutines.\n\nmy $context = Template::Context->new({\nLOADFILTERS => [\nMyTemplate::Filters->new(),\nTemplate::Filters->new(),\n],\n});\n\nSTASH\nThe STASH option can be used to specify a Template::Stash object or sub-class which will take\nresponsibility for managing template variables.\n\nmy $stash = MyOrg::Template::Stash->new({ ... });\nmy $context = Template::Context->new({\nSTASH => $stash,\n});\n\nDEBUG\nThe DEBUG option can be used to enable various debugging features of the Template::Context\nmodule.\n\nuse Template::Constants qw( :debug );\n\nmy $template = Template->new({\nDEBUG => DEBUGCONTEXT | DEBUGDIRS,\n});\n\ntemplate($name)\nReturns a compiled template by querying each of the LOADTEMPLATES providers (instances of\nTemplate::Provider, or sub-class) in turn.\n\n$template = $context->template('header');\n\nOn error, a Template::Exception object of type '\"file\"' is thrown via \"die()\". This can be\ncaught by enclosing the call to \"template()\" in an \"eval\" block and examining $@.\n\neval { $template = $context->template('header') };\nif ($@) {\nprint \"failed to fetch template: $@\\n\";\n}\n\nplugin($name, \\@args)\nInstantiates a plugin object by querying each of the LOADPLUGINS providers. The default\nLOADPLUGINS provider is a Template::Plugins object which attempts to load plugin modules,\naccording the various configuration items such as PLUGINBASE, LOADPERL, etc., and then\ninstantiate an object via new(). A reference to a list of constructor arguments may be passed as\nthe second parameter. These are forwarded to the plugin constructor.\n\nReturns a reference to a plugin (which is generally an object, but doesn't have to be). Errors\nare thrown as Template::Exception objects with the type set to '\"plugin\"'.\n\n$plugin = $context->plugin('DBI', 'dbi:msql:mydbname');\n\nfilter($name, \\@args, $alias)\nInstantiates a filter subroutine by querying the LOADFILTERS providers. The default\nLOADFILTERS provider is a Template::Filters object.\n\nAdditional arguments may be passed by list reference along with an optional alias under which\nthe filter will be cached for subsequent use. The filter is cached under its own $name if $alias\nis undefined. Subsequent calls to \"filter($name)\" will return the cached entry, if defined.\nSpecifying arguments bypasses the caching mechanism and always creates a new filter. Errors are\nthrown as Template::Exception objects with the type set to '\"filter\"'.\n\n# static filter (no args)\n$filter = $context->filter('html');\n\n# dynamic filter (args) aliased to 'padright'\n$filter = $context->filter('format', '%60s', 'padright');\n\n# retrieve previous filter via 'padright' alias\n$filter = $context->filter('padright');\n\nprocess($template, \\%vars)\nProcesses a template named or referenced by the first parameter and returns the output\ngenerated. An optional reference to a hash array may be passed as the second parameter,\ncontaining variable definitions which will be set before the template is processed. The template\nis processed in the current context, with no localisation of variables performed. Errors are\nthrown as Template::Exception objects via \"die()\".\n\n$output = $context->process('header', { title => 'Hello World' });\n\ninclude($template, \\%vars)\nSimilar to process(), but using localised variables. Changes made to any variables will only\npersist until the \"include()\" method completes.\n\n$output = $context->include('header', { title => 'Hello World' });\n\ninsert($template)\nThis method returns the source content of a template file without performing any evaluation. It\nis used to implement the \"INSERT\" directive.\n\nthrow($errortype, $errormessage, \\$output)\nRaises an exception in the form of a Template::Exception object by calling \"die()\". This method\nmay be passed a reference to an existing Template::Exception object; a single value containing\nan error message which is used to instantiate a Template::Exception of type '\"undef\"'; or a pair\nof values representing the exception \"type\" and \"info\" from which a Template::Exception object\nis instantiated. e.g.\n\n$context->throw($exception);\n$context->throw(\"I'm sorry Dave, I can't do that\");\n$context->throw('denied', \"I'm sorry Dave, I can't do that\");\n\nThe optional third parameter may be a reference to the current output buffer. This is then\nstored in the exception object when created, allowing the catcher to examine and use the output\nup to the point at which the exception was raised.\n\n$output .= 'blah blah blah';\n$output .= 'more rhubarb';\n$context->throw('yack', 'Too much yacking', \\$output);\n\ncatch($exception, \\$output)\nCatches an exception thrown, either as a reference to a Template::Exception object or some other\nvalue. In the latter case, the error string is promoted to a Template::Exception object of\n'\"undef\"' type. This method also accepts a reference to the current output buffer which is\npassed to the Template::Exception constructor, or is appended to the output buffer stored in an\nexisting Template::Exception object, if unique (i.e. not the same reference). By this process,\nthe correct state of the output buffer can be reconstructed for simple or nested throws.\n\ndefineblock($name, $block)\nAdds a new block definition to the internal BLOCKS cache. The first argument should contain the\nname of the block and the second a reference to a Template::Document object or template\nsub-routine, or template text which is automatically compiled into a template sub-routine.\n\nReturns a true value (the sub-routine or Template::Document reference) on success or undef on\nfailure. The relevant error message can be retrieved by calling the error() method.\n\ndefinefilter($name, \\&filter, $isdynamic)\nAdds a new filter definition by calling the store() method on each of the LOADFILTERS providers\nuntil accepted (in the usual case, this is accepted straight away by the one and only\nTemplate::Filters provider). The first argument should contain the name of the filter and the\nsecond a reference to a filter subroutine. The optional third argument can be set to any true\nvalue to indicate that the subroutine is a dynamic filter factory.\n\nReturns a true value or throws a '\"filter\"' exception on error.\n\ndefinevmethod($type, $name, $code)\nThis method is a wrapper around the Template::Stash definevmethod() method. It can be used to\ndefine new virtual methods.\n\n# define a new scalar (item) virtual method\n$context->definevmethod(\nitem => ucfirst => sub {\nmy $text = shift;\nreturn ucfirst $text;\n}\n)\n\ndefineview($name, \\%params)\nThis method allows you to define a named view.\n\n$context->defineview(\nmyview => {\nprefix => 'mytemplates/'\n}\n);\n\nThe view is then accessible as a template variable.\n\n[% myview.print(somedata) %]\n\ndefineviews($views)\nThis method allows you to define multiple named views. A reference to a hash array or list\nreference should be passed as an argument.\n\n$context->defineview({     # hash reference\nmyviewone => {\nprefix => 'mytemplatesone/'\n},\nmyviewtwo => {\nprefix => 'mytemplatestwo/'\n}\n});\n\nIf you're defining multiple views of which one or more are based on other views in the same\ndefinition then you should pass them as a list reference. This ensures that they get created in\nthe right order (Perl does not preserve the order of items defined in a hash reference so you\ncan't guarantee that your base class view will be defined before your subclass view).\n\n$context->defineview([     # list referenence\nmyviewone => {\nprefix => 'mytemplatesone/'\n},\nmyviewtwo => {\nprefix => 'mytemplatestwo/' ,\nbase   => 'myviewone',\n}\n]);\n\nThe views are then accessible as template variables.\n\n[% myviewone.print(somedata) %]\n[% myviewtwo.print(somedata) %]\n\nSee also the VIEWS option.\n\nstash()\nThis method returns the Template::Stash object used internally to manage template variables.\n\nlocalise(\\%vars)\nClones the stash to create a context with localised variables. Returns a reference to the newly\ncloned stash object which is also stored internally.\n\n$stash = $context->localise();\n\ndelocalise()\nRestore the stash to its state prior to localisation.\n\n$stash = $context->delocalise();\n\nvisit(\\%blocks)\nThis method is called by Template::Document objects immediately before they process their\ncontent. It is called to register any local \"BLOCK\" definitions with the context object so that\nthey may be subsequently delivered on request.\n\nleave()\nCompliment to the visit() method. Called by Template::Document objects immediately after they\nprocess their content.\n\nview()\nThis method creates a Template::View object bound to the context.\n\nreset()\nClears the local BLOCKS cache of any \"BLOCK\" definitions. Any initial set of BLOCKS specified as\na configuration item to the constructor will be reinstated.\n\ndebugging($flag, @args)\nThis method is used to control debugging output. It is used to implement the DEBUG directive.\n\nThe first argument can be \"on\" or \"off\" to enable or disable debugging respectively. The\nnumerical values 0 and 1 can also be used if you prefer.\n\n$context->debugging('on');\n\nAlternately, the first argument can be \"format\" to define a new debug message format. The second\nargument should be the format string which can contain any of the $file, $line or $text symbols\nto indicate where the relevant values should be inserted.\n\n# note single quotes to prevent interpolated of variables\n$context->debugging( format => '## $file line $line: $text' );\n\nThe final use of this method is to generate debugging messages themselves. The first argument\nshould be \"msg\", followed by a reference to a hash array of value to insert into the debugging\nformat string.\n\n$context->debugging(\nmsg => {\nline => 20,\nfile => 'example.tt',\ntext => 'Trampoline! Trampoline!',\n}\n);\n\nAUTOLOAD\nAn \"AUTOLOAD\" method provides access to context configuration items.\n\n$stash     = $context->stash();\n$tflag     = $context->trim();\n$epflag    = $context->evalperl();\n...\n",
                "subsections": []
            },
            "AUTHOR": {
                "content": "Andy Wardley <abw@wardley.org> <http://wardley.org/>\n",
                "subsections": []
            },
            "COPYRIGHT": {
                "content": "Copyright (C) 1996-2013 Andy Wardley. All Rights Reserved.\n\nThis module is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "Template, Template::Document, Template::Exception, Template::Filters, Template::Plugins,\nTemplate::Provider, Template::Service, Template::Stash\n",
                "subsections": []
            }
        }
    }
}