{
    "mode": "perldoc",
    "parameter": "Template::Service",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Template%3A%3AService/json",
    "generated": "2026-07-06T16:33:06Z",
    "synopsis": "use Template::Service;\nmy $service = Template::Service->new({\nPREPROCESS  => [ 'config', 'header' ],\nPOSTPROCESS => 'footer',\nERROR        => {\nuser     => 'user/index.html',\ndbi      => 'error/database',\ndefault  => 'error/default',\n},\n});\nmy $output = $service->process($templatename, \\%replace)\n|| die $service->error(), \"\\n\";",
    "sections": {
        "NAME": {
            "content": "Template::Service - General purpose template processing service\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Template::Service;\n\nmy $service = Template::Service->new({\nPREPROCESS  => [ 'config', 'header' ],\nPOSTPROCESS => 'footer',\nERROR        => {\nuser     => 'user/index.html',\ndbi      => 'error/database',\ndefault  => 'error/default',\n},\n});\n\nmy $output = $service->process($templatename, \\%replace)\n|| die $service->error(), \"\\n\";\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"Template::Service\" module implements an object class for providing a consistent template\nprocessing service.\n\nStandard header (PREPROCESS) and footer (POSTPROCESS) templates may be specified which are\nprepended and appended to all templates processed by the service (but not any other templates or\nblocks \"INCLUDE\"d or \"PROCESS\"ed from within). An ERROR hash may be specified which redirects\nthe service to an alternate template file in the case of uncaught exceptions being thrown. This\nallows errors to be automatically handled by the service and a guaranteed valid response to be\ngenerated regardless of any processing problems encountered.\n\nA default \"Template::Service\" object is created by the Template module. Any \"Template::Service\"\noptions may be passed to the Template new() constructor method and will be forwarded to the\nTemplate::Service constructor.\n\nuse Template;\n\nmy $template = Template->new({\nPREPROCESS  => 'header',\nPOSTPROCESS => 'footer',\n});\n\nSimilarly, the \"Template::Service\" constructor will forward all configuration parameters onto\nother default objects (e.g. Template::Context) that it may need to instantiate.\n\nA \"Template::Service\" object (or subclass) can be explicitly instantiated and passed to the\nTemplate new() constructor method as the SERVICE item.\n\nuse Template;\nuse Template::Service;\n\nmy $service = Template::Service->new({\nPREPROCESS  => 'header',\nPOSTPROCESS => 'footer',\n});\n\nmy $template = Template->new({\nSERVICE => $service,\n});\n\nThe \"Template::Service\" module can be sub-classed to create custom service handlers.\n\nuse Template;\nuse MyOrg::Template::Service;\n\nmy $service = MyOrg::Template::Service->new({\nPREPROCESS  => 'header',\nPOSTPROCESS => 'footer',\nCOOLOPTION  => 'enabled in spades',\n});\n\nmy $template = Template->new({\nSERVICE => $service,\n});\n\nThe Template module uses the Template::Config service() factory method to create a default\nservice object when required. The $Template::Config::SERVICE package variable may be set to\nspecify an alternate service module. This will be loaded automatically and its new() constructor\nmethod called by the service() factory method when a default service object is required. Thus\nthe previous example could be written as:\n\nuse Template;\n\n$Template::Config::SERVICE = 'MyOrg::Template::Service';\n\nmy $template = Template->new({\nPREPROCESS  => 'header',\nPOSTPROCESS => 'footer',\nCOOLOPTION  => 'enabled in spades',\n});\n",
            "subsections": []
        },
        "METHODS": {
            "content": "new(\\%config)\nThe \"new()\" constructor method is called to instantiate a \"Template::Service\" object.\nConfiguration parameters may be specified as a HASH reference or as a list of \"name => value\"\npairs.\n\nmy $service1 = Template::Service->new({\nPREPROCESS  => 'header',\nPOSTPROCESS => 'footer',\n});\n\nmy $service2 = Template::Service->new( ERROR => 'error.html' );\n\nThe \"new()\" method returns a \"Template::Service\" 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::Service::ERROR package variable.\n\nmy $service = Template::Service->new(\\%config)\n|| die Template::Service->error();\n\nmy $service = Template::Service->new(\\%config)\n|| die $Template::Service::ERROR;\n\nprocess($input, \\%replace)\nThe \"process()\" method is called to process a template specified as the first parameter, $input.\nThis may be a file name, file handle (e.g. \"GLOB\" or \"IO::Handle\") or a reference to a text\nstring containing the template text. An additional hash reference may be passed containing\ntemplate variable definitions.\n\nThe method processes the template, adding any PREPROCESS or POSTPROCESS templates defined, and\nreturns the output text. An uncaught exception thrown by the template will be handled by a\nrelevant ERROR handler if defined. Errors that occur in the PREPROCESS or POSTPROCESS\ntemplates, or those that occur in the main input template and aren't handled, cause the method\nto return \"undef\" to indicate failure. The appropriate error message can be retrieved via the",
            "subsections": [
                {
                    "name": "error",
                    "content": "$service->process('myfile.html', { title => 'My Test File' })\n|| die $service->error();\n\ncontext()\nReturns a reference to the internal context object which is, by default, an instance of the\nTemplate::Context class.\n"
                }
            ]
        },
        "CONFIGURATION OPTIONS": {
            "content": "The following list summarises the configuration options that can be provided to the\n\"Template::Service\" new() constructor. Please consult Template::Manual::Config for further\ndetails and examples of each configuration option in use.\n\nPREPROCESS, POSTPROCESS\nThe PREPROCESS and POSTPROCESS options may be set to contain the name(s) of template files\nwhich should be processed immediately before and/or after each template. These do not get added\nto templates processed into a document via directives such as \"INCLUDE\" \"PROCESS\", \"WRAPPER\",\netc.\n\nmy $service = Template::Service->new({\nPREPROCESS  => 'header',\nPOSTPROCESS => 'footer',\n};\n\nMultiple templates may be specified as a reference to a list. Each is processed in the order\ndefined.\n\nmy $service = Template::Service->new({\nPREPROCESS  => [ 'config', 'header' ],\nPOSTPROCESS => 'footer',\n};\n\nPROCESS\nThe PROCESS option may be set to contain the name(s) of template files which should be processed\ninstead of the main template passed to the \"Template::Service\" process() method. This can be\nused to apply consistent wrappers around all templates, similar to the use of PREPROCESS and\nPOSTPROCESS templates.\n\nmy $service = Template::Service->new({\nPROCESS  => 'content',\n};\n\n# processes 'content' instead of 'foo.html'\n$service->process('foo.html');\n\nA reference to the original template is available in the \"template\" variable. Metadata items can\nbe inspected and the template can be processed by specifying it as a variable reference (i.e.\nprefixed by '\"$\"') to an \"INCLUDE\", \"PROCESS\" or \"WRAPPER\" directive.\n\nExample \"PROCESS\" template:\n\n<html>\n<head>\n<title>[% template.title %]</title>\n</head>\n<body>\n[% PROCESS $template %]\n</body>\n</html>\n\nERROR\nThe ERROR (or \"ERRORS\" if you prefer) configuration item can be used to name a single template\nor specify a hash array mapping exception types to templates which should be used for error\nhandling. If an uncaught exception is raised from within a template then the appropriate error\ntemplate will instead be processed.\n\nIf specified as a single value then that template will be processed for all uncaught exceptions.\n\nmy $service = Template::Service->new({\nERROR => 'error.html'\n});\n\nIf the ERROR or ERRORS item is a hash reference the keys are assumed to be exception types and\nthe relevant template for a given exception will be selected. A \"default\" template may be\nprovided for the general case.\n\nmy $service = Template::Service->new({\nERRORS => {\nuser     => 'user/index.html',\ndbi      => 'error/database',\ndefault  => 'error/default',\n},\n});\n\nAUTORESET\nThe AUTORESET option is set by default and causes the local \"BLOCKS\" cache for the\nTemplate::Context object to be reset on each call to the Template process() method. This ensures\nthat any \"BLOCK\"s defined within a template will only persist until that template is finished\nprocessing.\n\nDEBUG\nThe DEBUG option can be used to enable debugging messages from the \"Template::Service\" module by\nsetting it to include the \"DEBUGSERVICE\" value.\n\nuse Template::Constants qw( :debug );\n\nmy $template = Template->new({\nDEBUG => DEBUGSERVICE,\n});\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Andy Wardley <abw@wardley.org> <http://wardley.org/>\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (C) 1996-2007 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::Context\n",
            "subsections": []
        }
    },
    "summary": "Template::Service - General purpose template processing service",
    "flags": [],
    "examples": [],
    "see_also": []
}