NAME
App::Scaffolder::Template - Represent a template for App::Scaffolder
VERSION
version 0.002000
SYNOPSIS
use Path::Class::Dir;
use App::Scaffolder::Template;
my $template = App::Scaffolder::Template->new({
name => 'name_of_the_template',
path => [
Path::Class::Dir->new('/first/path/belonging/to/template'),
Path::Class::Dir->new('/second/path/belonging/to/template'),
]
});
my @files = $template->process({
target => Path::Class::Dir->new('target', 'directory'),
variables => {
variable_value => 'a variable value',
}
});
DESCRIPTION
App::Scaffolder::Template represents a template. A template consists of one or more directories containing files that should be copied to a target directory when processing it. If a file has a .tt
extension, it will be passed through Template before it is written to a target file without the .tt
suffix. Thus the template
foo
|-- subdir
| |-- template.txt.tt
| `-- sub.txt
|-- top-template.txt.tt
`-- bar.txt
would result in the following structure after processing:
output
|-- subdir
| |-- template.txt
| `-- sub.txt
|-- top-template.txt
`-- bar.txt
The path of a file in the template may also contain variables, which are delimited by ___
and replaced with values from the variables
passed to process
:
# Template file path:
___dir___/___name___.txt
# Given $variables = { dir => 'directory', name => 'some_name' }, the
# following file will be created in the target directory:
directory/some_name.txt
This can be useful if parts of the output file path are not constant.
METHODS
new
Constructor, creates new instance.
Parameters
This method expects its parameters as a hash reference.
- name
-
Name of the template.
- path
-
Search path for files that belong to the template. If more than one file has the same relative path, only the first one will be used, so it is possible to override files that come 'later' in the search path.
add_path_entry
Push another directory on the search path.
get_path
Getter for the template file search path.
Result
The template file search path.
get_name
Getter for the name.
Result
The name.
process
Process the template.
Parameters
This method expects its parameters as a hash reference.
- target
-
Target directory where the output should be stored.
- variables
-
Hash reference with variables that should be made available to templates.
- overwrite
-
By default, existing files will not be overwritten. Passing a truthy value for the
overwrite
parameter changes this.
Result
A list with the created files on succes, an exception otherwise.
get_content_for
Given a file from the template and some template variables, read and potentially process the file and return the content the resulting file should have.
Parameters
This method expects positional parameters.
- file
-
Input file from the template.
- variables
-
Variables that should be made available to the template.
Result
The content for the corresponding target file.
replace_file_path_variables
Replace parts inside a Path::Class::File-based path that match ___<name>___
with a value from a hash.
Parameters
This method expects positional parameters.
- path
-
Path to the file which may contain placeholders.
- variables
-
Hash reference with values that should replace the placeholders.
Result
The processed file path.
get_template_files
Getter for the file that belong to the template.
Result
A hash reference containing hash references with information about the files.
AUTHOR
Manfred Stock <mstock@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Manfred Stock.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.