- NAME
- SYNOPSIS
- EXPORTS
- DEPENDENCIES
- DESCRIPTION
- EXAMPLES
- PUBLIC INTERFACE

- new
- Default_Context
- set_directives
- set_directive
- get_directive
- compile
- compile_text
- get_ctx
- use
- unuse
- eval
- eval_fields
- get_compiled_value
- get_value_str
- value_to_string
- get_value
- end_pos
- block_pos
- substr_pos

- PACKAGE INTERNALS

- _create_context
- _find_regions
- _remove_context
- _invoke
- _slurp
- _padding

- AUTHORS
- COPYRIGHT

# NAME

Parse::Template::Base -

--title=Parse::Template::Base

# SYNOPSIS

--title=Parse::Template::Base

# EXPORTS

Nothing exported by default

--title=Parse::Template::Base

# DEPENDENCIES

This module requires these other modules and libraries:

        Data::Hub::Courier
        Parse::Padding
        Parse::Template::Arguments
        Error::Logical
        Error::Programatic
        Perl::Module
        Data::Hub::Util
        Parse::Template::ArgumentStack
        Algorithm::KeyGen

--title=Parse::Template::Base

# DESCRIPTION

--title=Parse::Template::Base

# EXAMPLES

This example:

      # Simple replacement
      use Parse::Template::Standard;
      my $t = 'Hello [#name]';
      my $p = new Parse::Template::Standard();
      my $o = $p->compile_text(\$t, {name => 'World'});
      return $$o;

will return:

      Hello World

This example:

      # Variable expansion
      use Parse::Template::Standard;
      my $t = 'Hello [#name]';
      my $p = new Parse::Template::Standard();
      my $o = $p->compile_text(\$t, {name => '[#next]', next => 'World'});
      return $$o;

will return:

      Hello World

This example:

      # Nested elements
      use Parse::Template::Standard;
      my $t = 'Hello [#data/name]';
      my $p = new Parse::Template::Standard();
      my $o = $p->compile_text(\$t, {data => {name => 'World'}});
      return $$o;

will return:

      Hello World

This example:

      # Dynamic expansion
      use Parse::Template::Standard;
      my $t = 'Hello [#[#name]]';
      my $p = new Parse::Template::Standard();
      my $o = $p->compile_text(\$t, {name => 'next', next => 'World'});
      return $$o;

will return:

      Hello World

--title=Parse::Template::Base

# PUBLIC INTERFACE

## new

Constructor

  options:

        -begin => $begin_str          # Begins match (default '[#')
        -end => $end_str              # Ends match (default ']')
        -close => $close_str          # Closes a block (default 'end')
        -directive => $directive_str  # Identifies a directive (default ':')
        -out => \$output              # Where output is written

## Default_Context

Default (initial) context

## set_directives

Set the directive handlers for each given name-value pair

## set_directive

Set the directive handler for a given name-value pair

## get_directive

Get a directive handler

  where:

        $name   directive name
        $idx    0 for begin, 1 for end

## compile

Main invokation method

## compile_text

Main invokation method

## get_ctx

Get the current context from the context stack

## use

Add a namespace on to the local stack

  Calls to use must match calls to un-use within the same context

## unuse

Remove the last used namespace from the local stack

  Calls to use must match calls to un-use within the same context

## eval

Evaluate the given element

## eval_fields

Walk through fields locically returning the [uncompiled] value

  This routine does the work of processing the variable spec with consideration of the `x ? y : z`, `&&` and `||` operations. For instance:

       a
       a || b
       t ? a : b
       t ? f || a : b && c
       f ? f || c : b && a

  See also: `src/test/perl/t-eval-fields.pl`

## get_compiled_value

Get a variable as a string and compile if necessary

## get_value_str

Get a variable value as a string

  See also: get_value

## value_to_string

Get the string representation of a value

## get_value

Get a variable value

  `$addr` is passed by-reference because we will update it when applying local path information.

## end_pos

Find the position of the corresponding end directive

  All directives nested SHOULD be either inline (they do not slurp) or blocks (they have an end directive).

  All nested directives MUST also have their own end directive. This is not currently the case, so we do our best. Take this scenario:

        [#:set foo]
          This is a block
        [#:end set]

  Which is just fine. However, this is difficult:

        [#:set foo]
          [#:set bar = 'Bar']
          This is a block
        [#:end set]

  Because we don't know that the nested `:set` directive is inline. So, we ask for the `$to_eof` parameter. If this is true, then we will return the normal end position, that is -1 when nested occurences are not paired up. Otherwise we will return the end position of the *last* corresponding end directive. This is the best we can do without more information, and passing or garnering that information is considered too expensive an operation at this level.

  Provisions have been made only considering the `:set` and `:into` directives. -Ryan 11/2012

## block_pos

Return the begin and end block positions

  Similar in spirit to substr_pos except this routine uses regular expressions to ensure a nexted '[#abcd' does not count as a nested '[#ab' and this routine does not track all sub-indexes.

## substr_pos

Return positions of begin and end markers

  This routine recognizes `$begin_marker` and `$end_marker` as a balanced pair.

  Example 1:

        $begin_marker = [#
        $end_marker   = ]
        a [#b [#c] [#d]] e
          ^            ^
        returns [[2, 15]]

  Example 2:

        $begin_marker = [#if
        $end_marker   = [#end if]
        [#if true]do something[#end if]
        ^                     ^
        returns [[0, 22]]

  This example:

        use Parse::Template::Base;
        my $p = new Parse::Template::Base;
        my $text = '<<>>';
        my @pos = $p->substr_pos(\$text, '<', '>', 0);
        join ',', map {join '-', @$_} @pos;

  will return:

        0-3,1-2

--title=Parse::Template::Base

# PACKAGE INTERNALS

## _create_context

Add a new context on to the context stack

  where `%args` can be any combination of:

        text      => \$text,    # Template text
        path      => $path,     # Template path
        name      => $name,     # Template name
        out       => \$out,     # Where to write the result
        scope     => $scope,    # Stack index limit (invokation is being deferred)
        regions   => \@regions, # Known regions (\$text is being re-used)
        esc       => $chars     # Escape chars in resolved values

## _find_regions

Glean the begin and end indexes into template text

  returns:

        [\@match1, \@match2, ...]
      where each C<\@match> is:

        [
          $beg,       # Begin index
          $end,       # End index
          $nested,    # Has nested regions (1|undef)
        ]

## _remove_context

Remove the current context from the context stack

## _invoke

Implementation method

  See _create_context for `%args`

## _slurp

Slurp the contents of for block-type directive

  If `$to_eof` is a true value and an end directive is not found, `_slurp` will read to the end of the template text.

  If `$to_eol` is not defined, it is true, which means the newlines after the closing tag will be consumed.

## _padding

Get number of preceeding and trailing whitespace characters

  Returns an array of widths: ($w1, $w2)

        $w1 = Number of preceeding whitespace characters
        $w2 = Number of trailing whitespace characters

  Returns an (0, 0) if non-whitespace characters are immediately found in the preceeding or trailing regions.

  We will look up to 80 characters in front of the current position.

--title=Parse::Template::Base

# AUTHORS

        Ryan Gies <ryangies@cpan.org>

--title=Parse::Template::Base

# COPYRIGHT

        Copyright (C) 2014-2016 by Ryan Gies. All rights reserved.
        Copyright (C) 2006-2013 by Livesite Networks, LLC. All rights reserved.
        Copyright (C) 2000-2005 by Ryan Gies. All rights reserved.
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
        * The origin of this software must not be misrepresented; you must not
        claim that you wrote the original software. If you use this software in a
        product, an acknowledgment in the product documentation would be
        appreciated but is not required.
        * Altered source versions must be plainly marked as such, and must not be
        misrepresented as being the original software.
        * The name of the author may not be used to endorse or promote products
        derived from this software without specific prior written permission.

        THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
        WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
        MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
        EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
        EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
        OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
        IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
        OF SUCH DAMAGE.
        To the best of our knowledge, no patented algorithms have been used. However, we
        do not have the resources to carry out a patent search, and therefore cannot
        give any guarantee of the above statement.

--title=Parse::Template::Base