NAME
Data::Xslate - Templatize your data.
SYNOPSIS
use Data::Xslate;
my $xslate = Data::Xslate->new();
my $new_data = $xslate->render(
{
user => {
login => 'john',
email => '<: $login :>@example.com',
name => 'John',
},
email => {
to => '=:user.email',
subject => 'Hello <: $user.name :>!',
},
},
);
DESCRIPTION
This module provides a syntax for templatizing data structures.
The most likely use-case is adding some flexibility to configuration files.
SUBSTITUTION
{
foo => 14,
bar => '=:foo',
}
# { foo=>14, bar=>14 }
This injects the target value. This can be used for any data type. For example we can substitute an array:
{
foo => [1,2,3],
bar => '=:foo',
}
# { foo=>[1,2,3], bar=>[1,2,3] }
TEMPLATING
{
foo => 'green',
bar => 'It is <: $foo :>!',
}
# { foo=>'green', bar=>'It is green!' }
The syntax for templating is provided by Text::Xslate, so there is a lot of power here including being able to do math and string mangling.
SCOPE
When using either "SUBSTITUTION" or "TEMPLATING" you specify a key to be included. This key is found using scope-aware rules where the key is searched for in a similar fashion to how you'd expect when dealing with lexical variables in programming.
For example, you can refer to a key in the same scope:
{ a=>1, b=>'=:a' }
You may refer to a key in a lower scope:
{ a=>{ b=>1 }, c=>'=:a.b' }
You may refer to a key in a higher scope:
{ a=>{ b=>'=:c' }, c=>1 }
You may refer to a key in a higher scope that is nested:
{ a=>{ b=>'=:c.d' }, c=>{ d=>1 } }
The logic behind this is pretty flexible, so more complex use cases will just work like you would expect.
If you'd rather avoid this scoping you can prepend any key with a dot, .
, and it will be looked for at the root hash of the config tree only.
NESTED KEYS
When setting a key value the key can point deeper into the structure by separating keys with a dot, .
. Consider this:
{ a=>{ b=>1 }, 'a.b'=>2 }
Which produces:
{ a=>{ b=>2 } }
ARGUMENTS
Any arguments you pass to new
, which this class does not directly handle, will be used when creating the "xslate" object. So, any arguments which Text::Xslate supports may be set. For example:
my $xslate = Data::Xslate->new(
substitution_tag => ']]', # A Data::Xslate argument.
verbose => 2, # A Text::Xslate option.
);
substitution_tag
The string to look for at the beginning of any string value which signifies "SUBSTITUTION". Defaults to =:
.
METHODS
render
my $data_out = $xslate->render( $data_in );
AUTHOR
Aran Clary Deltac <bluefeet@gmail.com>
ACKNOWLEDGEMENTS
Thanks to ZipRecruiter for encouraging their employees to contribute back to the open source ecosystem. Without their dedication to quality software development this distribution would not exist.
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.