| 1 | package Any::Template::Backend::Text::MicroMason; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Text::MicroMason; | ||
| 5 | use Any::Template::Backend; | ||
| 6 | use vars qw(@ISA); | ||
| 7 | @ISA = qw(Any::Template::Backend); | ||
| 8 | |||
| 9 | use vars qw($VERSION); | ||
| 10 | $VERSION = sprintf"%d.%03d", q$Revision: 1.7 $ =~ /: (\d+)\.(\d+)/; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($class, $options) = @_; | ||
| 14 | my $attributes = $options->{Options}{Attributes} || {}; | ||
| 15 | my $mixins = $options->{Options}{Mixins} || []; | ||
| 16 | my $compile_options = _compile_options($options); | ||
| 17 | my $factory = new Text::MicroMason(@$mixins, %$attributes); | ||
| 18 | my $self = { | ||
| 19 | engine => $factory->compile(%$compile_options) | ||
| 20 | }; | ||
| 21 | return bless($self, $class); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub native_object { | ||
| 25 | my $self = shift; | ||
| 26 | return $self->{engine}; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub process_to_string { | ||
| 30 | my ($self, $data, $ref_buffer) = @_; | ||
| 31 | die("data must be a hashref") unless(ref $data eq "HASH"); | ||
| 32 | $$ref_buffer = $self->{engine}->(%$data); | ||
| 33 | return 1; | ||
| 34 | } | ||
| 35 | |||
| 36 | # | ||
| 37 | # This marshalls the Any::Template ctor options into the form required for Text::MicroMason | ||
| 38 | # | ||
| 39 | sub _compile_options { | ||
| 40 | my $at_options = shift; | ||
| 41 | my %options; | ||
| 42 | if(exists $at_options->{String}) { | ||
| 43 | $options{text} = $at_options->{String}; | ||
| 44 | } | ||
| 45 | elsif(exists $at_options->{Filename}) { | ||
| 46 | $options{file} = $at_options->{Filename}; | ||
| 47 | } | ||
| 48 | elsif(exists $at_options->{Filehandle}) { | ||
| 49 | $options{text} = _fh_to_string($at_options->{Filehandle}); | ||
| 50 | } | ||
| 51 | return \%options; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _fh_to_string { | ||
| 55 | my $fh = shift; | ||
| 56 | local $/ = undef; | ||
| 57 | $fh = $$fh if(ref $fh eq 'GLOB'); | ||
| 58 | my $string = <$fh>; | ||
| 59 | return $string; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub DUMP {} | ||
| 63 | sub TRACE {} | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | Any::Template::Backend::Text::MicroMason - Any::Template backend for Text::MicroMason | ||
| 70 | |||
| 71 | =head1 SYNOPSIS | ||
| 72 | |||
| 73 | use Any::Template; | ||
| 74 | my $template = new Any::Template( | ||
| 75 | Backend => 'Text::MicroMason', | ||
| 76 | Options => { | ||
| 77 | Attributes => {global_vars => 1}, #MicroMason %attribs | ||
| 78 | Mixins => [qw(-HTMLTemplate -Filters)] #Specify mixins | ||
| 79 | }, | ||
| 80 | File => 'page.tmpl' | ||
| 81 | ); | ||
| 82 | my $output = $template->process($data); | ||
| 83 | |||
| 84 | =head1 DESCRIPTION | ||
| 85 | |||
| 86 | Attributes may be passed to Text::MicroMason in the {Options}{Attributes} key. | ||
| 87 | The {Options}{Mixins} key is used to pass mixins to Text::MicroMason. | ||
| 88 | |||
| 89 | Inputs from a file and string are provided natively by Text::MicroMason. | ||
| 90 | Input from a filehandle uses the default implementation (which is to read into a string). | ||
| 91 | |||
| 92 | Output to filehandle is based on the default implementation of capturing output in a string and writing | ||
| 93 | this to a filehandle, so watch out for this if your output is very large. Output to a file uses the native | ||
| 94 | Text::MicroMason implementation. | ||
| 95 | |||
| 96 | Output to a coderef uses the default implementation of buffering all the output in a string and passing this to a coderef, | ||
| 97 | so again beware of the memory consumption if the output is large. | ||
| 98 | |||
| 99 | =head1 SEE ALSO | ||
| 100 | |||
| 101 | L<Any::Template>, L<Any::Template::Backend>, L<Text::MicroMason> | ||
| 102 | |||
| 103 | =head1 VERSION | ||
| 104 | |||
| 105 | $Revision: 1.7 $ on $Date: 2006/05/08 12:28:00 $ by $Author: mattheww $ | ||
| 106 | |||
| 107 | =head1 AUTHOR | ||
| 108 | |||
| 109 | John Alden <cpan _at_ bbc _dot_ co _dot_ uk> | ||
| 110 | |||
| 111 | =head1 COPYRIGHT | ||
| 112 | |||
| 113 | (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||
| 114 | |||
| 115 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | # vim:noet |