Catmandu-Template

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME

    Catmandu::Template - Catmandu modules for working with templates

MODULES

      * Catmandu::Exporter::Template

      * Catmandu::Fix::template

AUTHOR

    Nicolas Steenlant, <nicolas.steenlant at ugent.be>

CONTRIBUTOR

    Vitali Peil, <vitali.peil at uni-bielefeld.de>

lib/Catmandu/Exporter/Template.pm  view on Meta::CPAN

package Catmandu::Exporter::Template;

use Catmandu::Sane;
use Catmandu::Util qw(is_string);
use Catmandu;
use Template;
use Storable qw(freeze);
use Moo;
use namespace::clean;

our $VERSION = '0.14';

lib/Catmandu/Exporter/Template.pm  view on Meta::CPAN

    unless ($self->_before_done) {
        $self->fh->print($XML_DECLARATION) if $self->xml;
        $self->_process($self->template_before) if $self->template_before;
        $self->_before_done(1);
    }
    $self->_process($self->template_after) if $self->template_after;
}

=head1 NAME

Catmandu::Exporter::Template - a TT2 Template exporter in Catmandu style

=head1 SYNOPSIS

    # From the command line
    echo '{"colors":["red","green","blue"]}' | 
        catmandu convert JSON to Template --template `pwd`/xml.tt

    where xml.tt like:

    <colors>
    [% FOREACH c IN colors %]
       <color>[% c %]</color>
    [% END %]
    </colors>

    # From perl
    use Catmandu::Exporter::Template;

    my $exporter = Catmandu::Exporter::Template->new(
                fix => 'myfix.txt'
                xml => 1,
                template_before => '<path>/header.xml' ,
                template => '<path>/record.xml' ,
                template_after => '<path>/footer.xml' ,
           );

    $exporter->add_many($arrayref);
    $exporter->add_many($iterator);
    $exporter->add_many(sub { });

lib/Catmandu/Template.pm  view on Meta::CPAN

![Test status](https://github.com/LibreCat/Catmandu-RIS/actions/workflows/linux.yml/badge.svg)
[![Coverage](https://coveralls.io/repos/LibreCat/Catmandu-Template/badge.png?branch=master)](https://coveralls.io/r/LibreCat/Catmandu-Template)
[![CPANTS kwalitee](http://cpants.cpanauthors.org/dist/Catmandu-Template.png)](http://cpants.cpanauthors.org/dist/Catmandu-Template)

=end markdown

=head1 MODULES

=over

=item * L<Catmandu::Exporter::Template>

=item * L<Catmandu::Fix::template>

=back

=head1 AUTHOR

Nicolas Steenlant, C<< <nicolas.steenlant at ugent.be> >>

=head1 CONTRIBUTOR

t/Catmandu-Exporter-Template.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Exporter::Template';
    use_ok $pkg;
}
require_ok $pkg;

#only template_before, no records
{
    my $file     = "";
    my $template = <<EOF;
Author: [% author %]
EOF

t/file_extension.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
use Catmandu::Exporter::Template;

my $file = "";

my $exporter = Catmandu::Exporter::Template->new(
    file      => \$file,
    template  => 'out.html',
    start_tag => "<%",
    end_tag   => "!>"
);

my $data = {author => "brian d foy", title => "Mastering Perl",};

$exporter->add($data);

my $result = <<EOF;
Author: brian d foy
Title: "Mastering Perl"
EOF

is($file, $result, "html extension");

my $exporter2 = Catmandu::Exporter::Template->new(
    file      => \$file,
    template  => 'out.tt',
    start_tag => "<%",
    end_tag   => "!>"
);

$exporter2->add($data);

is($file, $result, "tt extension");

t/options.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
use Catmandu::Exporter::Template;

my $file     = "";
my $template = <<EOF;
Author: <% author !>
Title: "<% title !>"
EOF

my $exporter = Catmandu::Exporter::Template->new(
    file      => \$file,
    template  => \$template,
    start_tag => "<%",
    end_tag   => "!>"
);
my $data = {author => "brian d foy", title => "Mastering Perl",};

$exporter->add($data);

my $result = <<EOF;

t/options.t  view on Meta::CPAN

Title: "Mastering Perl"
EOF

is($file, $result, "Exported Format");

my $template2 = <<EOF;
Author: <? author ?>
Title: "<? title ?>"
EOF

my $exporter2 = Catmandu::Exporter::Template->new(
    file      => \$file,
    template  => \$template2,
    tag_style => "php"
);
$exporter2->add($data);

is($file, $result, "Tag style ok");

# TT caching
$exporter = Catmandu::Exporter::Template->new(
    template  => \$template,
    start_tag => "<%",
    end_tag   => "!>"
);
$exporter2 = Catmandu::Exporter::Template->new(
    template  => \$template,
    start_tag => "<%",
    end_tag   => "!>"
);

ok $exporter->_tt == $exporter2->_tt,
    'tt engines wirth equal arguments get reused';

$exporter2 = Catmandu::Exporter::Template->new(
    template  => \$template,
    start_tag => "<%",
    end_tag   => "%>"
);

ok $exporter->_tt != $exporter2->_tt,
    'tt engines with equal arguments get reused';

$exporter = Catmandu::Exporter::Template->new(
    template     => \$template,
    INCLUDE_PATH => '/tmp/a',
);

$exporter2 = Catmandu::Exporter::Template->new(
    template     => \$template,
    INCLUDE_PATH => '/tmp/a',
);

ok $exporter->_tt == $exporter2->_tt,
    'tt engines wirth equal arguments get reused';

$exporter2 = Catmandu::Exporter::Template->new(
    template     => \$template,
    INCLUDE_PATH => '/tmp/b',
);

ok $exporter->_tt != $exporter2->_tt,
    'tt engines wirth equal arguments get reused';

done_testing;



( run in 0.295 second using v1.01-cache-2.11-cpan-0f795438458 )