package App::ZofCMS::Plugin::FileToTemplate; use warnings; use strict; our $VERSION = '0.0101'; use Data::Transformer; use File::Spec; sub new { bless {}, shift } my $Template_Dir; sub process { my ( $self, $template, $query, $config ) = @_; $Template_Dir = $config->conf->{templates}; my $t = Data::Transformer->new( normal => \&callback ); $t->traverse( $template ); } sub callback { my $in = shift; while ( my ( $t ) = $$in =~ /]+)>/ ) { my $tag_result; my $file = File::Spec->catfile( $Template_Dir, $t ); if ( open my $fh, '<', $file ) { $tag_result = do { local $/; <$fh>; }; } else { $tag_result = $!; } $$in =~ s/]+>/$tag_result/; } if ( my ( $t ) = $$in =~ /]+)>/ ) { my $tag_result; my $file = File::Spec->catfile( $Template_Dir, $t ); $tag_result = do $file or $tag_result = "ERROR: $! $@"; $$in = $tag_result; } } 1; __END__ =head1 NAME App::ZofCMS::Plugin::FileToTemplate - read or do() files into ZofCMS Templates =head1 SYNOPSIS In your ZofCMS Template: plugins => [ qw/FileToTemplate/ ], t => { foo => '', }, In you L template: =head1 DESCRIPTION The module is a plugin for L; it provides functionality to either read (slurp) or C files and stick them in place of "tags".. read on to understand more. This documentation assumes you've read L, L and L =head1 ADDING THE PLUGIN plugins => [ qw/FileToTemplate/ ], Unlike many other plugins to run this plugin you barely need to include it in the list of plugins to execute. =head1 TAGS t => { foo => '', bar => '', }, Anywhere in your ZofCMS template you can use two "tags" that this plugin provides. Those "tags" will be replaced - depending on the type of tag - with either the contents of the file or the last value returned by the file. Both tags are in format: opening angle bracket, name of the tag in capital letters, semicolon, filename, closing angle bracket. The filename is relative to your "templates" directory, i.e. the directory referenced by C key in Main Config file. =head2 C<< >> t => { foo => '', }, The C<< >> reads (slurps) the contents of the file and tag is replaced with those contents. You can have several of these tags as values. Be careful reading in large files with this tag. Mnemonic: Bile Bo Bemplate Bead. =head2 C<< >> t => { foo => '', }, The C<< >> tag will C your file and the last returned value will be assigned to the B, in other words, having C<< foo => '', >> and C<< foo => ' blah blah blah', >> is the same. Using this tag, for example, you can add large hashrefs or config hashrefs into your templates without clobbering them. Note that if the C cannot find your file or compilation of the file fails, the value with the tag will be replaced by the error message. Mnemomic: Bile Bo Bemplate Bo. =head1 NON-CORE PREREQUISITES The plugin requires one non-core module: L =head1 AUTHOR 'Zoffix, C<< <'zoffix at cpan.org'> >> (L, L, L) =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc App::ZofCMS::Plugin::FileToTemplate You can also look for information at: =over 4 =item * RT: CPAN's request tracker L =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * Search CPAN L =back =head1 COPYRIGHT & LICENSE Copyright 2008 'Zoffix, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut