Perl Template Toolkit

Book description

Among the many different approaches to "templating" with Perl--such as Embperl, Mason, HTML::Template, and hundreds of other lesser known systems--the Template Toolkit is widely recognized as one of the most versatile. Like other templating systems, the Template Toolkit allows programmers to embed Perl code and custom macros into HTML documents in order to create customized documents on the fly. But unlike the others, the Template Toolkit is as facile at producing HTML as it is at producing XML, PDF, or any other output format. And because it has its own simple templating language, templates can be written and edited by people who don't know Perl. In short, the Template Toolkit combines the best features of its competitors, with ease-of-use and flexibility, resulting in a technology that's fast, powerful and extensible, and ideally suited to the production and maintenance of web content and other dynamic document systems.In Perl Template Toolkit you'll find detailed coverage of this increasingly popular technology. Written by core members of the technology's development team, the book guides you through the entire process of installing, configuring, using, and extending the Template Toolkit. It begins with a fast-paced but thorough tutorial on building web content with the Template Toolkit, and then walks you through generating and using data files, particularly with XML. It also provides detailed information on the Template Toolkit's modules, libraries, and tools in addition to a complete reference manual.Topics in the book include:

  • Getting started with the template toolkit
  • The Template language
  • Template directives
  • Filters
  • Plugins
  • Extending the Template Toolkit
  • Accessing databases
  • XML
  • Advanced static web page techniques
  • Dynamic web content and web applications
The only book to cover this important tool, Perl Template Toolkit is essential reading for any Perl programmer who wants to create dynamic web content that is remarkably easy to maintain. This book is your surefire guide to implementing this fast, flexible, and powerful templating system.

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Audience
    2. About this Book
    3. Conventions Used in This Book
    4. Comments and Questions
    5. Acknowledgments
      1. Andy Wardley
      2. Darren Chamberlain
      3. David Cross
  3. 1. Getting Started with the Template Toolkit
    1. What the Template Toolkit Does
    2. The Templating Ecosystem
      1. The Template Toolkit Is for More Than HTML
      2. The Template Toolkit Lets You Choose Your Separation
      3. Nonprogrammers Can Maintain Templates
      4. The Template Toolkit Is Easy to Extend
    3. Installing the Template Toolkit
      1. Downloading
      2. Installing
    4. Documentation and Support
      1. Viewing the Documentation
      2. Overview of the Documentation
      3. Accessing the Mailing List
    5. Using the Template Toolkit
      1. tpage
      2. ttree
      3. The Template Module
        1. Template configuration options
      4. Apache::Template Module
    6. The Template Toolkit Language
    7. Template Variables
      1. Complex Variables
      2. Lists
      3. Hashes
        1. Nesting list and hash definitions
      4. Dynamic Variables
      5. Virtual Methods
    8. Template Directives
      1. Variable Directives
      2. Template Processing Directives
      3. Loops
      4. Conditionals
      5. Filters, Plugins, and Macros
    9. Integrating and Extending the Template Toolkit
  4. 2. Building a Complete Web Site Using the Template Toolkit
    1. Getting Started
      1. A Single Page
      2. A “Hello World” HTML Template
      3. Processing Templates with tpage
    2. Template Components
      1. Headers and Footers
        1. The PROCESS directive
        2. The INSERT directive
      2. Benefits of Modularity
    3. Defining Variables
      1. Configuration Template
        1. Comments
        2. Variable values
      2. Loading the Configuration Template
        1. Merging directives
    4. Generating Many Pages
      1. Creating a Project Directory
      2. ttree Configuration File
      3. Running ttree for the First Time
      4. Using a Build Script
      5. ttree Configuration Directory
      6. Calling ttree Through the Build Script
      7. A Place for Everything, and Everything in Its Place
      8. Running the Build Script
      9. Skipping Unmodified Templates
    5. Adding Headers and Footers Automatically
      1. Defining META Tags
    6. More Template Components
      1. The INCLUDE Directive
      2. Setting Default Values
        1. The DEFAULT directive
        2. Expressions
          1. = versus = =
          2. Setting variables using expressions
          3. Setting variables using directives
      3. The IF Directive
    7. Wrapper and Layout Templates
      1. The WRAPPER Directive
      2. Using an Automatic Wrapper Template
      3. Using Layout Templates
      4. Layout Example
        1. Side-effect wrappers
        2. Separating layout concerns
        3. Plugins and the USE directive
    8. Menu Components
      1. Simple Menu Template
        1. The BLOCK directive
      2. Component Libraries
        1. The EXPOSE_BLOCKS option
      3. The FOREACH Directive
    9. Defining and Using Complex Data
      1. Structured Configuration Templates
      2. Layered Configuration Templates
      3. Choosing Global Variables Wisely
      4. Passing Around Data Structures
    10. Assessment
  5. 3. The Template Language
    1. Template Syntax
      1. Text and Directives
        1. Template parser
        2. Parse errors
        3. Caching templates
        4. Flexible syntax
      2. Template Tags
      3. Interpolated Variables
      4. Comments
      5. Whitespace Chomping
        1. Chomping flags
        2. Chomping options
        3. Chomping constants
      6. Multiple Directive Tags
      7. Side-Effect Notation
      8. Capturing Directive Output
      9. Template Filenames
    2. Template Variables
      1. What’s in a Name?
      2. Simple Data Types
      3. Complex Data Types
        1. Lists
        2. Hashes
      4. Dot Operator
        1. Compound dot operations
        2. Interpolated variables names
        3. Private variables
      5. Dynamic Data Types
        1. Subroutines
        2. Objects
        3. Passing arguments
        4. Pointless arguments
        5. Named parameters
        6. Mixing dynamic and static data
        7. Returning values
        8. Error handling
      6. Special Variables
        1. template
        2. component
        3. loop
        4. error
        5. content
        6. global
        7. view, item
      7. Variable Scope
      8. Compile-Time Constant Folding
    3. Virtual Methods
      1. Scalar Virtual Methods
        1. chunk(size)
        2. defined
        3. hash
        4. length
        5. list
        6. match(pattern)
        7. repeat(n)
        8. replace(search, replace)
        9. size
        10. split(pattern)
      2. List Virtual Methods
        1. first(n)
        2. grep(pattern)
        3. join(delimiter)
        4. last(n)
        5. max
        6. merge(list)
        7. pop
        8. reverse
        9. shift
        10. size
        11. slice(from, to)
        12. sort, nsort
        13. splice(offset, length, list)
        14. unique
        15. unshift(item)
        16. push(item)
      3. Hash Virtual Methods
        1. defined(key)
        2. each
        3. exists(key)
        4. import(hash)
        5. item(key)
        6. keys
        7. list
        8. size
        9. sort, nsort
        10. values
      4. Defining New Virtual Methods
        1. Stash package variables
        2. Stash and context methods
  6. 4. Template Directives
    1. Accessing Variables
      1. GET
      2. SET
      3. CALL
      4. DEFAULT
    2. Accessing External Templates and Files
      1. INSERT
      2. INCLUDE
      3. PROCESS
      4. WRAPPER
    3. Defining Local Template Blocks
    4. Loops
      1. FOREACH
        1. Complex data
        2. Importing hash array items
        3. Iterating over entries in a hash array
        4. The loop iterator object
        5. Nested FOREACH loops
      2. WHILE
        1. Flow control: NEXT and LAST
    5. Conditionals
      1. IF, ELSIF, ELSE, and UNLESS
      2. SWITCH and CASE
    6. Filters
    7. Plugins
      1. USE
    8. Macros
      1. MACRO
    9. Template Metadata
      1. META
    10. Exception Handling
      1. TRY / THROW / CATCH / FINAL
      2. CLEAR
    11. Flow Control
      1. RETURN
      2. STOP
      3. NEXT
      4. LAST
    12. Debugging
    13. Perl Blocks
      1. PERL
      2. RAWPERL
  7. 5. Filters
    1. Using Filters
    2. Standard Template Toolkit Filters
      1. collapse
      2. eval / evaltt
      3. format(fmt)
      4. html
      5. html_break / html_para_break
      6. html_entity
      7. html_line_break
      8. html_para
      9. indent(pad)
      10. latex(outputType)
      11. lcfirst
      12. lower
      13. null
      14. perl / evalperl
      15. redirect(file, options)
      16. remove(string)
      17. repeat(iterations)
      18. replace(search, replace)
      19. stderr
      20. stdout(options)
      21. trim
      22. truncate(length)
      23. ucfirst
      24. upper
      25. uri
  8. 6. Plugins
    1. Using Plugins
    2. Standard Template Toolkit Plugins
      1. Autoformat
      2. CGI
      3. Datafile
      4. Date
      5. Directory
      6. DBI
      7. Dumper
      8. File
      9. Format
      10. GD
      11. HTML
      12. Image
      13. Iterator
      14. Pod
      15. String
      16. Table
      17. URL
      18. Wrap
      19. XML::DOM
      20. XML::RSS
      21. XML::Style
      22. XML::XPath
  9. 7. Anatomy of the Template Toolkit
    1. Template Modules
      1. Template::Service
      2. Template::Context
      3. Template::Stash
      4. Template::Provider
      5. Template::Parser
      6. Template::Grammar
      7. Template::Directive
      8. Template::Namespace::Constants
      9. Template::Document
    2. The Runtime Engine
    3. Module Interfaces
      1. Template’s process Method
        1. The process method
          1. Input template
          2. Template variables
          3. Redirecting template output
          4. Processing options
        2. The error method
      2. Template::Config
        1. load
        2. preload
        3. instdir
      3. Template::Constants
        1. :status
        2. :error
        3. :chomp
        4. :debug
      4. Template::Base
        1. new
        2. error
        3. debug
      5. Template::Context
        1. stash
        2. insert, include, and process
        3. template
        4. plugin and filter
        5. define_filter
      6. Template::Provider
        1. fetch($name)
        2. store($name, $template)
        3. paths
      7. Template::Stash
        1. get, set
        2. clone, declone
      8. Template::Filters
        1. new
        2. fetch
        3. store
      9. Template::Plugin
        1. load
        2. new
        3. error
      10. Template::Plugins
        1. new
        2. fetch
      11. Template::Parser and Template::Grammar
      12. Template::Directive
      13. Template::Document
        1. new
        2. process
        3. write_perl_file
        4. AUTOLOAD
      14. Template::Exception
      15. Template::Iterator
  10. 8. Extending the Template Toolkit
    1. Using and Implementing Noncore Components
      1. A Provider That Can Fetch Files over HTTP
      2. Restricting Access to Plugins
      3. A chrooted Provider
    2. Creating Filters
      1. Static Filters
      2. Dynamic Filters
      3. Template::Plugin::Filter
      4. Writing New Filters
        1. Digest::MD5
        2. Text::Bastardize
        3. Text::FIGlet
        4. Normalizing HTML: HTML::Clean
    3. Creating Plugins
      1. The Template::Plugin Module
      2. Installing Functions into the Stash from Within a Plugin
      3. Defining Filters from Within a Plugin
      4. Defining New Virtual Methods from Within a Plugin
      5. Writing New Plugins
        1. A simple wrapper plugin
        2. A more complex wrapper plugin
        3. A plugin that sends mail
        4. GoogleSearch
        5. Normalizing URLs
    4. Building a New Frontend
      1. Mail::Template
      2. Custom Apache Handlers
    5. Changing the Language
      1. Building the Grammar
      2. Extending the Existing Grammar
      3. Replacing the Default Grammar
        1. Template::Simple
        2. The Template::Simple grammar
  11. 9. Accessing Databases
    1. Using the DBI Plugin
      1. Simple Database Access with the DBI Plugin
      2. A More Complex Example: Web Access Logs
    2. Using Class::DBI
    3. Using DBIx::Table2Hash
      1. Writing Your Own Database Abstraction Layer
  12. 10. XML
    1. Simple XML Processsing
    2. Creating XML Documents
      1. Modeling Data About a TV Show
      2. DTD for a TV Show
      3. XML Template
    3. Processing RSS Files with XML.RSS
    4. Processing XML Documents with XML.DOM
    5. Processing XML Documents with XML.XPath
    6. Processing XML Documents with XML.LibXML
    7. Using Views to Transform XML Content
  13. 11. Advanced Static Web Page Techniques
    1. Getting Started
      1. Directory Structure
      2. Web Server Configuration
      3. ttree Configuration
      4. Simple pre_process and wrapper Templates
      5. Creating the Build Script
      6. A First HTML Page
      7. Automating the Project Configuration Process
    2. Library Templates
      1. Configuration Templates
      2. Layout Templates
        1. Page wrappers
        2. Layout components
    3. Content Templates
      1. HTML Pages
      2. CSS and Other Non-HTML Pages
      3. Content Components
      4. Debugging Pages
    4. Navigation Components
      1. Adding Site Structure
        1. Map nodes
        2. XML site map
        3. Selective mapmaking
      2. Walking the Structure
      3. Building a Nested Menu
      4. A Stacked Menu
      5. Bread-Crumb Trail
      6. Previous and Next Pages
    5. Structuring Page Content
      1. Defining Sections
        1. Section headers
        2. Section wrappers
        3. Sections and subsections
      2. A Table of Contents
        1. Anchor points
        2. Better page macros
        3. Generating the table of contents
        4. Reusing menu components
        5. Adding the table of contents automatically
      3. Declarative Markup Using XML
        1. XML page content
        2. XML page wrapper
        3. XML view templates
    6. Creating a New Skin
      1. Creating a Skin
      2. Custom Navigation Components
        1. Nested menu
        2. Menu elements
        3. Stacked menu
        4. Other page components
  14. 12. Dynamic Web Content and Web Applications
    1. CGI Scripts
      1. Simple CGI Script
        1. Using standard templates
      2. Using the DATA Section
      3. Using the CGI Module
        1. Setting cookies
      4. CGI Script Web Application
        1. CGI script
          1. Preparation
          2. Application
          3. Presentation
        2. Template components
        3. Page templates
    2. CGI Templates
      1. Using the CGI Plugin
      2. Web Programming in Templates
        1. Dispatching CGI script
        2. Main control template
        3. Additional control templates
        4. Perl or template?
    3. Apache and mod_perl
      1. Apache::Template
      2. Custom Apache Handler
    4. A Complete Web Application
      1. Storage
        1. TTBook::H2G2::Database
      2. Configuration
      3. Application
      4. Apache mod_perl Interface Module
      5. Apache Configuration
  15. A. Appendix: Configuration Options
    1. Template Toolkit Configuration Options
      1. ABSOLUTE
      2. ANYCASE
      3. AUTO_RESET
      4. BLOCKS
      5. CACHE_SIZE
      6. COMPILE_EXT
      7. COMPILE_DIR
      8. CONSTANTS
      9. CONSTANT_NAMESPACE
      10. NAMESPACE
      11. CONTEXT
      12. DEBUG
      13. DEBUG_FORMAT
      14. DEFAULT
      15. DELIMITER
      16. ERROR
      17. EVAL_PERL
      18. FACTORY
      19. FILTERS
      20. GRAMMAR
      21. INCLUDE_PATH
      22. INTERPOLATE
      23. LOAD_FILTERS
      24. LOAD_PERL
      25. LOAD_PLUGINS
      26. LOAD_TEMPLATES
      27. OUTPUT_PATH
      28. OUTPUT
      29. PARSER
      30. PLUGIN_BASE
      31. PLUGINS
      32. PRE_CHOMP, POST_CHOMP
      33. PRE_DEFINE, VARIABLES
      34. PRE_PROCESS, POST_PROCESS
      35. PROCESS
      36. RECURSION
      37. RELATIVE
      38. SERVICE
      39. STASH
      40. START_TAG, END_TAG
      41. TAG_STYLE
      42. TOLERANT
      43. TRIM
      44. VARIABLES, PRE_DEFINE
      45. V1DOLLAR
    2. Apache::Template Configuration Options
      1. TT2Tags
      2. TT2PreChomp
      3. TT2PostChomp
      4. TT2Trim
      5. TT2AnyCase
      6. TT2Interpolate
      7. TT2IncludePath
      8. TT2Absolute
      9. TT2Relative
      10. TT2Delimiter
      11. TT2PreProcess
      12. TT2PostProcess
      13. TT2Process
      14. TT2Default
      15. TT2Error
      16. TT2EvalPerl
      17. TT2LoadPerl
      18. TT2Recursion
      19. TT2PluginBase
      20. TT2AutoReset
      21. TT2CacheSize
      22. TT2CompileExt
      23. TT2CompileDir
      24. TT2Debug
      25. TT2Headers
      26. TT2Params
      27. TT2ServiceModule
  16. Index
  17. About the Authors
  18. Colophon
  19. Copyright

Product information

  • Title: Perl Template Toolkit
  • Author(s): Darren Chamberlain, Andy Wardley, David Cross
  • Release date: December 2003
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596004767