# Copyright 2013-2014 Stefan Goebel. # # This file is part of Newcomen. # # Newcomen is free software: you can redistribute it and/or modify it under the terms of the GNU # General Public License as published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # Newcomen is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # # You should have received a copy of the GNU General Public License along with Newcomen. If not, see # . package Newcomen::Renderer::TemplateToolkit; our $VERSION = 2014052501; use namespace::autoclean; use Moose '-meta_name' => '_moose_meta'; use MooseX::StrictConstructor; use Template; use Newcomen::Util::Hash; use Newcomen::Util; extends 'Newcomen::Renderer::Backend'; with 'Newcomen::Role::Attribute::Core'; with 'Newcomen::Role::Attribute::Config'; sub BUILD { shift -> _config () -> add_default ({ 'renderer' => { 'templatetoolkit' => { 'template' => undef, 'use_cache' => 1, }, } }); } has '_cache' => ( 'is' => 'ro', 'isa' => 'HashRef[Template]', 'default' => sub {{}}, 'init_arg' => undef, 'traits' => ['Hash'], 'handles' => { '_set_cache' => 'set', '_get_cache' => 'get', '_cached' => 'exists', }, ); override '_do_render' => sub { my $self = shift; my $page = shift; my $opts = shift; my $out = undef; my $conf = $self -> _config () -> get (['renderer', 'templatetoolkit']); $conf = Newcomen::Util::Hash::merge ($conf, $opts -> data ()); my $tmpl = (delete $conf -> {'template' }) // 'default.tt'; my $cache = (delete $conf -> {'use_cache'}); my $t = undef; if ($cache) { my $key = Newcomen::Util::data_sha1 ($conf); if ($self -> _cached ($key)) { $t = $self -> _get_cache ($key); } else { $t = Template -> new ($conf) or confess 'Template Toolkit error: ' . Template -> error (); $self -> _set_cache ($key, $t); } } else { $t = Template -> new ($conf) or confess 'Template Toolkit error: ' . Template -> error (); } my $vars = { 'core' => $self -> _core (), 'catalog' => $self -> _core () -> catalog (), 'config' => $self -> _core () -> config (), 'crawler' => $self -> _core () -> crawler (), 'plugins' => $self -> _core () -> plugins (), 'site' => $self -> _core () -> site (), 'url' => $self -> _core () -> url (), 'page' => $page, 'c' => $self -> _core () -> config (), 'u' => $self -> _core () -> url (), 'p' => $page, }; $t -> process ($tmpl, $vars, \$out) or confess 'Template Toolkit error: ' . $t -> error (); return $out; }; __PACKAGE__ -> _moose_meta () -> make_immutable (); 1; __END__ #################################################################################################### =head1 NAME Newcomen::Renderer::TemplateToolkit - Template Toolkit renderer backend. =head1 DESCRIPTION Renderer backends are usually not called directly. See L for the frontend documentation. This module provides a renderer backend using L