# perldoc > Template::Document

---
type: CommandReference
command: Template::Document
mode: perldoc
section: 
source: perldoc
---

## Quick Reference

- `Template::Document->new({ BLOCK => ..., DEFBLOCKS => ..., METADATA => ... })` — create a compiled template document
- `$doc->process($context)` — execute the template and return output
- `$doc->author()` — access metadata via AUTOLOAD (e.g., `$doc->title`)
- `Template::Document->as_perl(\%config)` — generate Perl source for the template

## Name

**Template::Document** — Compiled template document object

## Synopsis

perl
use Template::Document;

$doc = Template::Document->new({
    BLOCK => sub { # some perl code; return $some_text },
    DEFBLOCKS => {
        header => sub { # more perl code; return $some_text },
        footer => sub { # blah blah blah; return $some_text },
    },
    METADATA => {
        author  => 'Andy Wardley',
        version => 3.14,
    }
}) || die $Template::Document::ERROR;

print $doc->process($context);
## Options (Constructor Parameters and Methods)

### Constructor Parameters

- `BLOCK` — reference to a Perl subroutine (or string of Perl code) that constitutes the main template block
- `DEFBLOCKS` — hash reference of named sub-blocks (each a subroutine reference or code string)
- `METADATA` — hash reference of arbitrary metadata items (e.g., author, version)

### Instance Methods

- `process($context)` — execute the template, returning text output; takes a `Template::Context` object
- `block()` — returns a reference to the main BLOCK subroutine
- `blocks()` — returns a reference to the hash of named DEFBLOCKS subroutines
- `variables()` — returns a hash reference of variables used in the template (requires `TRACE_VARS` option)
- `AUTOLOAD` — accesses metadata items as methods (e.g., `$doc->author()`)

### Class Methods

- `as_perl(\%config)` — generate a Perl source representation of the template
- `write_perl_file(\%config)` — write compiled Perl template to disk (used internally by `Template::Parser` when `COMPILE_EXT` is set)

## Examples

**Accessing template metadata from within a template:**

perl
# In a PRE_PROCESS template:
# template object is available as 'template'
<html>
<head>
<title>[% template.title %]</title>
</head>
...
## See Also

- [Template](https://perldoc.perl.org/Template)
- [Template::Parser](https://perldoc.perl.org/Template::Parser)