Dist::Zilla::Plugin::Templates::Manual - Templates plugin user manual
Templates
Version v0.6.0, released on 2015-10-31 21:54 UTC.
Dist-Zilla-Plugin-Templates (or just Templates) is a Dist-Zilla plugin allowing developers to insert fragments of Perl code into arbitrary text files, which become templates. When building a distribution with Dist::Zilla, Templates plugin evaluates these fragments, and replaces each fragment with result of its evaluation.
Dist-Zilla-Plugin-Templates
Dist-Zilla
Dist::Zilla
This is Templates user manual. Read this if you want to convert any text file into template.
If you are going to hack or extend Dist-Zilla-Plugin-Templates, read the module documentation. General topics like getting source, building, installing, bug reporting and some others are covered in the README.
In your dist.ini:
name = Assa version = 0.007 … [Templates] templates = :InstallModules ; ^ Template files, ; all .pm and .pod files in this example. package = MY ; ^ Evaluate Perl fragments in context ; of this package. prepend = use autodie ':all'; prepend = use Path::Tiny; ; ^ Code to execute in the beginning of ; each fragment. …
In lib/Assa.pm:
package Assa; our $VERSION = '{{ $dist->version }}'; … 1; =head1 RATIONALE {{ # Include content of doc/rationale.pod file # (with expanding templates, if any): include( "doc/rationale.pod" )->fill_in; }} =head1 EXAMPLES =head2 example.pl {{ # Include content of ex/example.pl file, # strip trailing newlines, # indent it to make it verbatim: include( "ex/example.pl" )->chomp->indent; }} =head1 COPYRIGHT AND LICENSE {{ $dist->license->notice }} =cut
TODO
$dist
Reference to Dist::Zilla object. Primary purpose of the variable is providing access to the distribution properties, like $dist->version, $dist->name, $dist->version, etc, but $dist also gives full access to Dist::Zilla object.
$dist->version
$dist->name
$plugin
Reference to the plugin (usually to the Templates, or to its descendant, if someone creates it). There is no much meaning in it, though. The variable is provided for compatibility with GatherDir::Template plugin. However, the variable can be used for debug or error reporting, e. g.:
GatherDir::Template
$plugin->log_debug( … ); $plugin->log( … );
include
include( $filename )
The function include returns a reference to Dist::Zilla::Plugin::Templates::File object (which is a descendant of Dist::Zilla::File::InMemory class). The object being evaluated in string context returns the file content, so
Dist::Zilla::Plugin::Templates::File
Dist::Zilla::File::InMemory
{{ include( 'ex/example.pl' )->content; }}
can be shortened to
{{ include( 'ex/example.pl' ); }}
with the same result.
Dist::Zilla::Plugin::Templates::File defines few methods which can be useful in Perl code and/or POD templates:
{{ include( 'ex/example.pl' )->indent; }} {{ include( 'doc/caveats.pod' )->fill_in . include( 'doc/feedback.pod' )->fill_in; }}
Since many Dist::Zilla::Plugin::Templates::File methods return self-reference, calls may be chained:
{{ include( 'ex/example.pl' )->fill_in->chomp->indent; }}
See Dist::Zilla::Plugin::Templates::File for the list of methods.
The plugin defines only one option templates. Other options (package, prepend, delimiter, …) come from TextTemplater role, so see TextTemplater role manual for details.
templates
package
prepend
delimiter
TextTemplater
Name of file finder to provide files to be treated as templates. The default value is :NoFiles. Use any of standard finders like :MainModule, :InstallModules, :AllFiles (see "default_finders" in Dist::Zilla::Role::FileFinderUser), or create your own finder with FileFinder::ByName and FileFinder::Filter plugins.
:NoFiles
:MainModule
:InstallModules
:AllFiles
FileFinder::ByName
FileFinder::Filter
Option may be specified several times, e. g.:
templates = :InstallModules templates = :TestFiles
Each selected file will be processed only once, even if a file "found" by more than one finder:
templates = :InstallModules templates = :AllFiles
template is an alias for templates.
template
Because I was not satisfied with existing solutions.
GatherDir::Template (shipped as a part of Dist::Zilla) combines two tasks: it adds files to distribution and does template processing. Such coupling introduces some limitations: All the templates must be in a separate directory, you cannot freely mix template and non-template files. If you use source manifest and adds files to distribution with Manifest::Read or GatherFromManifest plugins, you cannot manifest your templates — it causes "attempt to add filename multiple times" error.
Manifest::Read
GatherFromManifest
TemplateFiles solves this problem, but has its own limitations. It requires to list all the templates individually, you cannot use Dist::Zilla file finders to declare all install modules (or all tests, or all files, etc).
TemplateFiles
Both GatherDir::Template and TemplateFiles suffer from disadvantages of Dist::Zilla TextTemplate role: lack of control over Text::Template engine and awful error reporting.
TextTemplate
Text::Template
Thus, Templates plugin:
Uses TextTemplater role to provide better control over Text::Template engine and better error reporting.
Uses Dist::Zilla file finders to select files.
TODO:
Distribution builder.
Dist::Zilla::Role::TextTemplate
Bridge between Dist::Zilla and Text::Template, provides templating capabilities to Dist::Zilla plugins. It is a part of Dist::Zilla distribution. It has limited template engine control and awful error reporting (as of v5.037).
Dist::Zilla::Role::TextTemplater
An alternative to standard TextTemplate, it uses the same template engine, Text::Template, but provides better engine control and error reporting.
Dist::Zilla::Plugin::TemplateFiles
Alternative approach. It does not use file finders, so you have to specify every template file individually. It also uses Dist::Zilla standard TextTemplate role with all the subsequences.
Dist::Zilla::Plugin::GatherDir::Template
A combo of file gathering and templating capabilities. It uses standard TextTemplate role.
The great templating engine used by both TextTemplate and TextTemplater roles.
Dist::Zilla::Plugin::Templates
Van de Bugger <van.de.bugger@gmail.com>
Copyright (C) 2015 Van de Bugger
License GPLv3+: The GNU General Public License version 3 or later <http://www.gnu.org/licenses/gpl-3.0.txt>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
To install Dist::Zilla::Plugin::Templates, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dist::Zilla::Plugin::Templates
CPAN shell
perl -MCPAN -e shell install Dist::Zilla::Plugin::Templates
For more information on module installation, please visit the detailed CPAN module installation guide.