The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Template::Resolver - A powerful, and simple, library for resolving placeholders in templated files

VERSION

version 1.16

DESCRIPTION

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<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}

CONSTRUCTORS

new(\%entity, %options)

Creates a new resolver with properties from \%entity and %options if any. The available options are:

additional_transforms

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.

os

The operating system path format used when resolving ${TEMPLATE_os{xxx}} placeholders.

METHODS

resolve(%options)

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:

content

A string containing templated content.

filename

The name of a file containing templated content.

handle

A handle to a file containing templated content.

key

The template key, defaults to TEMPLATE.

AUTHOR

Lucas Theisen <lucastheisen@pastdev.com>

COPYRIGHT AND LICENSE

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.

SEE ALSO

Please see those modules/websites for more information related to this module.

SYNOPSIS # Basic example use Template::Resolver; my $resolver = Template::Resolver->new($entity); $resolver->resolve(file => '/path/to/file', key => 'REPLACEME');

  # 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 );