26 Apr 2022 16:22:07 UTC
- Distribution: Template-Toolkit
- Source (raw)
- Browse (raw)
- How to Contribute
- Issues (74)
- Testers (1566 / 27 / 0)
- KwaliteeBus factor: 2
- % Coverage
- License: perl_5
- Activity24 month
- Download (509.1KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version
- SEE ALSO
Template::Tools::ttree - Process entire directory trees of templates
ttree [options] [files]
The ttree script is used to process entire directory trees containing template files. The resulting output from processing each file is then written to a corresponding file in a destination directory. The script compares the modification times of source and destination files (where they already exist) and processes only those files that have been modified. In other words, it is the equivalent of 'make' for the Template Toolkit.
It supports a number of options which can be used to configure behaviour, define locations and set Template Toolkit options. The script first reads the .ttreerc configuration file in the HOME directory, or an alternative file specified in the TTREERC environment variable. Then, it processes any command line arguments, including any additional configuration files specified via the
When you run ttree for the first time it will ask you if you want it to create a .ttreerc file for you. This will be created in your home directory.
$ ttree Do you want me to create a sample '.ttreerc' file for you? (file: /home/abw/.ttreerc) [y/n]: y /home/abw/.ttreerc created. Please edit accordingly and re-run ttree
The purpose of this file is to set any global configuration options that you want applied every time ttree is run. For example, you can use the
linkoptions to provide regular expressions that specify which files should be ignored and which should be copied or linked rather than being processed as templates. You may also want to set flags like
recurseaccording to your preference.
A minimal .ttreerc:
# ignore these files ignore = \b(CVS|RCS)\b ignore = ^# ignore = ~$ # copy these files copy = \.(gif|png|jpg|pdf)$ # recurse into directories recurse # provide info about what's going on verbose
In most cases, you'll want to create a different ttree configuration file for each project you're working on. The
cfgoption allows you to specify a directory where ttree can find further configuration files.
cfg = /home/abw/.ttree
-fcommand line option can be used to specify which configuration file should be used. You can specify a filename using an absolute or relative path:
$ ttree -f /home/abw/web/example/etc/ttree.cfg $ ttree -f ./etc/ttree.cfg $ ttree -f ../etc/ttree.cfg
If the configuration file does not begin with
.or something that looks like a MS-DOS absolute path (e.g.
C:\\etc\\ttree.cfg) then ttree will look for it in the directory specified by the
$ ttree -f test1 # /home/abw/.ttree/test1
cfgoption can only be used in the .ttreerc file. All the other options can be used in the .ttreerc or any other ttree configuration file. They can all also be specified as command line options.
Remember that .ttreerc is always processed before any configuration file specified with the
-foption. Certain options like
libcan be used any number of times and accumulate their values.
For example, consider the following configuration files:
cfg = /home/abw/.ttree lib = /usr/local/tt2/templates
lib = /home/abw/web/example/templates/lib
When ttree is invoked as follows:
$ ttree -f myconfig
liboption will be set to the following directories:
Any templates located under /usr/local/tt2/templates will be used in preference to those located under /home/abw/web/example/templates/lib. This may be what you want, but then again, it might not. For this reason, it is good practice to keep the .ttreerc as simple as possible and use different configuration files for each ttree project.
srcoption is used to define the directory containing the source templates to be processed. It can be provided as a command line option or in a configuration file as shown here:
src = /home/abw/web/example/templates/src
Each template in this directory typically corresponds to a single web page or other document.
destoption is used to specify the destination directory for the generated output.
dest = /home/abw/web/example/html
liboption is used to define one or more directories containing additional library templates. These templates are not documents in their own right and typically comprise of smaller, modular components like headers, footers and menus that are incorporated into pages templates.
lib = /home/abw/web/example/templates/lib lib = /usr/local/tt2/templates
liboption can be used repeatedly to add further directories to the search path.
A list of templates can be passed to ttree as command line arguments.
$ ttree foo.html bar.html
It looks for these templates in the
srcdirectory and processes them through the Template Toolkit, using any additional template components from the
libdirectories. The generated output is then written to the corresponding file in the
If ttree is invoked without explicitly specifying any templates to be processed then it will process every file in the
srcdirectory. If the
-r(recurse) option is set then it will additionally iterate down through sub-directories and process and other template files it finds therein.
$ ttree -r
If a template has been processed previously, ttree will compare the modification times of the source and destination files. If the source template (or one it is dependant on) has not been modified more recently than the generated output file then ttree will not process it. The -a (all) option can be used to force ttree to process all files regardless of modification time.
$ tree -a
Any templates explicitly named as command line argument are always processed and the modification time checking is bypassed.
acceptoptions are used to specify Perl regexen to filter file names. Files that match any of the
ignoreoptions will not be processed. Remaining files that match any of the
linkregexen will be copied or linked to the destination directory. Files that reside in any of the
copy_dirdirectories are also copied. Remaining files that then match any of the
acceptcriteria are then processed via the Template Toolkit. If no
acceptparameter is specified then all files will be accepted for processing if not already copied or ignored.
# ignore these files ignore = \b(CVS|RCS)\b ignore = ^# ignore = ~$ # copy these files copy = \.(gif|png|jpg|pdf)$ # accept only .tt2 templates accept = \.tt2$
suffixoption is used to define mappings between the file extensions for source templates and the generated output files. The following example specifies that source templates with a
.tt2suffix should be output as
Or on the command line,
You can provide any number of different suffix mappings by repeating this option.
binmodeoption is used to set the encoding of the output file. For example use
--binmode=:utf8to set the output format to unicode.
depend_fileoptions allow you to specify how any given template file depends on another file or group of files. The
dependoption is used to express a single dependency.
$ ttree --depend foo=bar,baz
This command line example shows the
--dependoption being used to specify that the foo file is dependant on the bar and baz templates. This option can be used many time on the command line:
$ ttree --depend foo=bar,baz --depend crash=bang,wallop
or in a configuration file:
depend foo=bar,baz depend crash=bang,wallop
The file appearing on the left of the
=is specified relative to the
libdirectories. The file(s) appearing on the right can be specified relative to any of these directories or as absolute file paths.
$ ttree --depend foo=bar,/tmp/baz
To define a dependency that applies to all files, use
*on the left of the
$ ttree --depend *=header,footer
or in a configuration file:
Any templates that are defined in the
wrapperoptions will automatically be added to the list of global dependencies that apply to all templates.
depend_fileoption can be used to specify a file that contains dependency information.
$ ttree --depend_file=/home/abw/web/example/etc/ttree.dep
Here is an example of a dependency file:
# This is a comment. It is ignored. index.html: header footer menubar header: titlebar hotlinks menubar: menuitem # spanning multiple lines with the backslash another.html: header footer menubar \ sidebar searchform
Lines beginning with the
#character are comments and are ignored. Blank lines are also ignored. All other lines should provide a filename followed by a colon and then a list of dependant files separated by whitespace, commas or both. Whitespace around the colon is also optional. Lines ending in the
\character are continued onto the following line.
Files that contain spaces can be quoted. That is only necessary for files after the colon (':'). The file before the colon may be quoted if it contains a colon.
As with the command line options, the
*character can be used as a wildcard to specify a dependency for all templates.
* : config,header
ttree also provides access to the usual range of Template Toolkit options. For example, the
--post_chompttree options correspond to the
ttree -hfor a summary of the options available.
Andy Wardley <firstname.lastname@example.org>
With contributions from Dylan William Hardison (support for dependencies), Bryce Harrington (
relativeoptions), Mark Anderson (
debugoptions), Harald Joerg and Leon Brocard who gets everywhere, it seems.
2.68, distributed as part of the Template Toolkit version 2.19, released on 27 April 2007.
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Module Install Instructions
To install Template::Toolkit, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Template::Toolkit
For more information on module installation, please visit the detailed CPAN module installation guide.