Template::Resolver - A powerful, and simple, library for resolving placeholders in templated files
version 1.16
This module provides a powerful way to resolve placeholders inside of a templated file. It uses Template::Transformer to interpolate the the placeholder values. The provided template may refer to entity values directly (i.e. ${TEMPLATE{my.entity.value}}) or through transformations (i.e. ${TEMPLATE_perl{property("my.truthy") ? "true" : "false"}}). You may also loop over hash and array entities like this (newlines and indentation included for clarity):
${TEMPLATE{my.entity.value}}
${TEMPLATE_perl{property("my.truthy") ? "true" : "false"}}
${TEMPLATE<CLUB>:{my.clubs}}$ {TEMPLATE<MEMBER>:{<CLUB>.members}} ${TEMPLATE{<MEMBER>.name}} is a member of the ${TEMPLATE{<CLUB>.club_name}} club. ${TEMPLATE<MEMBER>:end} ${TEMPATE<CLUB>:end}
You may access the key when iterating over hashes:
${TEMPLATE<RESOURCE>:{my.resources}} Resource, ${TEMPLATE:<RESOURCE.key>} is ${TEMPLATE{<RESOURCE>.deployed_artifact}} ${TEMPLATE<RESOURCE>:end}
You may also access the index when iterating over arrays:
${TEMPLATE<CLUB>:{my.clubs}} Club at index ${TEMPLATE<CLUB.ix>} is ${TEMPLATE{<CLUB.name>}} ${TEMPLATE<CLUB>:end}
Creates a new resolver with properties from \%entity and %options if any. The available options are:
\%entity
%options
Additional custom transforms that will be added to the standard transforms. Must be a hashref containing transform name to sub reference mappings. The sub reference(s) will be called as a method(s) with a single parameter containing the contents of the placeholder.
The operating system path format used when resolving ${TEMPLATE_os{xxx}} placeholders.
${TEMPLATE_os{xxx}}
Will read the template and replace all placeholders prefixed by key. One of the options content, handle, or filename is required. The available options are:
key
content
handle
filename
A string containing templated content.
The name of a file containing templated content.
A handle to a file containing templated content.
The template key, defaults to TEMPLATE.
TEMPLATE
Lucas Theisen <lucastheisen@pastdev.com>
This software is copyright (c) 2015 by Lucas Theisen.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Please see those modules/websites for more information related to this module.
Template::Transformer
Template::Overlay
https://github.com/lucastheisen/template-resolver
# More complete example use Template::Resolver; $java_properties_file = <<'EOF'; # Simple value that will error if not present server_port = ${TEMPLATE{app.port}} # Simple value with a default (no error if not present) context_path = ${TEMPLATE{app.context_path:/myapp}} # Get an env var http_proxy = ${TEMPLATE_env{HTTP_PROXY}} # Translate a cygwin path with error if not present module_jar = ${TEMPLATE_os{app.module_addon1}} # Translate a cygwin path with default module_jar = ${TEMPLATE_os{app.module_addon2:/var/local/lib/mymodule.jar}} # Escape some xml (with blank default) html_header = ${TEMPLATE_xml_escape{app.header:}} # Run some perl https_enabled = ${TEMPLATE_perl{ property(app.use_https) ? 'true' : 'false'}} https_proxy = ${TEMPLATE_perl{ sprintf( 'https://%s:%d/', $ENV{HTTPS_PROXY_HOST}, $ENV{HTTPS_PROXY_PORT} )}} # Custom stuff db_user = ${TEMPLATE_customfetch{ 'dbuser' }} EOF my $entity = { app => { port => 80, module_addon1 => '/var/local/lib/mymodule.jar', use_https => 0, } }; my $resolver = Template::Resolver->new( $entity, additional_transforms => { customfetch => sub { #Do something here return 'mydbuser'; } }); my $transformed_result = $resolver->resolve( content => $java_properties_file );
To install Template::Resolver, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template::Resolver
CPAN shell
perl -MCPAN -e shell install Template::Resolver
For more information on module installation, please visit the detailed CPAN module installation guide.