<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># NAME

Template::Mustache - Drawing Mustaches on Perl for fun and profit

# VERSION

version 1.3.0

# SYNOPSIS

```perl
use Template::Mustache;

# one-shot rendering

print Template::Mustache-&gt;render(
    "Hello {{planet}}", 
);

# compile and re-use template

my $mustache = Template::Mustache-&gt;new(
    template =&gt; "Hello {{planet}}", 
);

print $mustache-&gt;render( { planet =&gt; "World!" } );
```

# DESCRIPTION

Template::Mustache is an implementation of the fabulous 
[Mustache](https://mustache.github.io/) templating
language for Perl.

This version of _Template::Mustache_ conforms to v1.1.3
of the [Mustache specs](https://github.com/mustache/spec).

Templates can be compiled and rendered on the spot via the
use of `render` called as a class method.

```
print Template::Mustache-&gt;render(
    "Hello {{planet}}", 
);
```

If you are considering re-using the same template many times, it's 
recommended to create a `Template::Mustache` object instead,
which will compile the template only once, and allow to render
it with different contexts.

```perl
my $mustache = Template::Mustache-&gt;new(
    template =&gt; "Hello {{planet}}", 
);

print $mustache-&gt;render( { planet =&gt; "World!" } );
```

# METHODS

## new( %arguments )

```perl
my $mustache = Template::Mustache-&gt;new(
    template   =&gt; "Hello {{planet}}",
    delimiters =&gt; [ qw/ ! ! / ],
);
```

Constructor.  

### arguments

- template =&gt; $string

    A Mustache template.

- template\_path =&gt; $path

    Instead of `template`, a `template_path` can be provided to read
    the template and the partials from the fielsystem instead. See
    the method `template_path` to see how this works.

- partials\_path =&gt; $path

    An optional filesystem path from which to gather partial
    templates.

- delimiters =&gt; \[ $opening\_tag, $closing\_tag \]

    An optional arrayref holding the pair of delimiters used by
    the template. Defaults to `{{ }}`.

- context =&gt; $context

    Context to use when rendering if not provided
    as a parameter to `render`. Defaults to the object
    itself.

- partials =&gt; $partials

    An optional hashref of partials to assign to the object. See
    the method `partials` for more details on its format.

    By default, if `partials_path` (or `template_path` is defined,
    the template will try to resolve the partials as filenames with 
    the file extension `.mustache`
    relative to that path.

    ```perl
    my $mustache = Template::Mustache-&gt;new( 
        partials =&gt; './root',
        template =&gt; '{{ &gt; ./my/partial }}',  # =&gt; file ./root/my/partial.mustache
    );
    ```

## render( $context )

```
print $mustache-&gt;render( $context );
```

Returns the rendered template, given the optionally provided context. Uses 
the object's `context attribute` if not provided.

### Context 

#### as a hashref

```perl
Template::Mustache-&gt;render( 'Hello {{ thing }}', { thing =&gt; 'World!' } );
```

If the value is a coderef, it will be invoked to generate the value
to be inserted in the template.

```perl
Template::Mustache-&gt;render(
    'it is {{ time }}', 
    { time =&gt; sub { scalar localtime } } 
);
```

If you want the value returned by the coderef to be 
interpolated as a Mustache template, a helper function is passed
as the last argument to the coderef.

```perl
Template::Mustache-&gt;render(
    'hello {{ place }}', 
    {
        place =&gt; sub { pop-&gt;('{{ planet }}') },
        planet =&gt; 'World',
    } 
);
```

The two previous interpolations work both for `{{variable}}`
definitions, but also for `{{#section}}`s.

```perl
print Template::Mustache-&gt;render(
    'I am {{#obfuscated}}resu{{/obfuscated}}',
    {
        obfuscated   =&gt; sub { pop-&gt;('{{'.reverse(shift).'}}') },
        user         =&gt; '({{logged_in_as}})',
        logged_in_as =&gt; 'Sam',
    }
);  # =&gt; 'I am (Sam)'
```

#### as an arrayref

```perl
Template::Mustache-&gt;render( 'Hello {{ 1 }}', [ 'Earth', 'World!' ] );
# =&gt; 'Hello World!
```

#### as an object

```perl
my $object = Something-&gt;new( ... );  

Template::Mustache-&gt;render( 'Hello {{ thing }}', $object );  # thing resolves to $object-&gt;thing
```

#### as a scalar

```
Template::Mustache-&gt;render( 'Hello {{ . }}', 'World!' );
```

#### no context

If no context is provided, it will default to the mustache object itself.
Which allows for definining templates as subclasses of _Template::Mustache_.

```perl
package My::Template;
use Moo;
extends 'Template::Mustache';

sub template  { 'Hello {{ planet }}!' }

sub planet { 'World' }


# later on
My::Template-&gt;new-&gt;render; # =&gt; Hello World!
```

#### multi-level variable

If the variable to be rendered is multi-level (e.g., `foo.bar`), it is
resolved recursively on the context.

```perl
# $foo-&gt;bar returns `{ baz =&gt; [ 'quux' ] }`

Template::Mustache-&gt;render( '{{ bar.baz.0 }}', $foo );  # =&gt; 'quux'
```

## render( $template, $context, $partials )

```perl
print Template::Mustache-&gt;render( $template, $context, $partials );

# equivalent to
Template::Mustache-&gt;new-&gt;( 
    template =&gt; $template, partials =&gt; $partials 
)-&gt;render( $context );
```

If invoked as a class method, `render` takes in the mustache template, and
an optional context and set of partials.

To pass in partials without a
context, set the context to `undef`.

```
print Template::Mustache-&gt;render( $template, undef, $partials );
```

## template( $template )

Accessor to the `template` attribute.

## template\_path( $path )

Accessor to the `template_path` attribute. If this attribute is 
set, the template will be set to the content of the provided file 
(if `$path` is a directory, the file is assumed to be the 
`Mustache.mustache` file local to that directory).

## partials\_path( $path ) 

Accessor the `partials_path` attribute. If partials were
not given as part of the object construction, when encountered
partials will be attempted to be read from that directory. 
The filename for a partial is its name with `.mustache` appended to it.

If `template_path` is defined, `partials_path` defaults to it.

## context( $context )

Accessor to the `context` attribute.

## delimiters( \[ $opening\_tag, $closing\_tag \] )

Accessor to the `delimiters` attribute.

## parsed

```perl
my $tree = $mustache-&gt;parsed;
```

Returns the [Template::Mustache::Token::Template](https://metacpan.org/pod/Template::Mustache::Token::Template) object representing
the parsed template.

## parser

Returns the instance of [Template::Mustache::Parser](https://metacpan.org/pod/Template::Mustache::Parser) used by the object.

## partials( { partial\_name =&gt; $partial, ... } )

```perl
my $mustache = Template::Mustache-&gt;new(
    template =&gt; "{{&gt; this }}",
    partials =&gt; { this =&gt; 'partials rock!' },
);

print $mustache-&gt;render; # =&gt; partials rock!
```

Add partial templates to the object. 

Partial values can be
strings holding Mustache templates;

A coderef can also be set instead of a hashref. In that
case, partial templates will be generated by invoking that
sub with the name of the partial as its argument.

```perl
my $mustache = Template::Mustache-&gt;new(
    template =&gt; "{{&gt; this }} and {{&gt; that }}",
    partials =&gt; sub { "a little bit of " . shift }
);
```

# CONSTANTS

## $GRAMMAR

```
print $Template::Mustache::GRAMMAR;
```

The [Parse::RecDescent](https://metacpan.org/pod/Parse::RecDescent) grammar used to parse Mustache templates.

# SEE ALSO

- [https://mustache.github.io](https://mustache.github.io)

    The main, pan-language site for _Mustache_.

- [https://mustache.github.io/mustache.5.html](https://mustache.github.io/mustache.5.html)

    Specs of the _Mustache_ DSL.

- [Text::Handlebars](https://metacpan.org/pod/Text::Handlebars)

    _Handlebars_ is another templating language heavily inspired and very similar to _Mustache_. [Text::Handlebars](https://metacpan.org/pod/Text::Handlebars)
    is an implementation of it using [Text::Xslate](https://metacpan.org/pod/Text::Xslate).

- [Mustache::Simple](https://metacpan.org/pod/Mustache::Simple)

    Another module implementing Mustache templates.

# AUTHORS

- Pieter van de Bruggen &lt;pvande@cpan.org&gt;
- Yanick Champoux &lt;yanick@cpan.org&gt; [![endorse](http://api.coderwall.com/yanick/endorsecount.png)](http://coderwall.com/yanick)
- Ricardo Signes &lt;rjbs@cpan.org&gt;

# COPYRIGHT AND LICENSE

This software is copyright (c) 2018, 2017, 2016, 2015, 2011 by Pieter van de Bruggen.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
</pre></body></html>