On Tue, May 5, 2009 at 7:24 PM, Tomas Doran <bobtf...@bobtfish.net> wrote:

>
> Sorry for pointing out the obvious, but this is just perl:
>
> my $output = delete $c->res->{body};
> opem(FH, ">file") or die;
> print FH $output;
> close(FH);
>
> will do what you want...


Hi Tom,

Thanks for your reply.

There are some things that aren't so obvious to me. Where would I place the
code represented in your snippet?  I can't just hang it out in the main body
of package hde::View::TTprint, since $c isn't available there. $c and
$outfile *are* available in sub process, once the format for the forward
statement is corrrect. So I *could* go ahead and perform the write-to-file
operation there, but that seems a bit muddled. It seems to me the clearest
place to write-to-file would be in sub output, but nothing seems to be
showing up there.

Even so, I did try writing from sub process, and it gave me an error, which
leads me to Question 2.
I'm trying to follow Template Toolkit's documented API, which offers these
configuration options, which I've used in my TTprint base class:

OUTPUT       => \&output,
OUTPUT_PATH  => [
        hde->path_to( 'root', 'static', 'published' ),
    ],

Supposedly, the processed template output will be written to the output file
relative to OUTPUT_PATH. But I get the error:

Caught exception in hde::View::TTprint->process "Cannot open
Test1.0.0/TOC.html for writing: No such file or directory"


BTW, this path already exists:
~/cat_work/hde/root/static/published/Test1.0.0

Please feel free to point out any obvious bit I've botched.

/dennis

Code summary:

In controller:
my $path_and_file = "Test1.0.0 /TOC.html";
$c->forward( $c->view('TTprint'), [ qq/$path_and_file/ ] );

In package hde::View::TTprint;
__PACKAGE__->config({
    TEMPLATE_EXTENSION => '.tt2',
    INCLUDE_PATH => [
        hde->path_to( 'root', 'src' ),
        hde->path_to( 'root', 'lib' ),
        hde->path_to( 'root', 'tt2' )
    ],
    PRE_PROCESS  => 'config/main',
    WRAPPER      => 'site/wrapper',
    OUTPUT       => \&output,
    OUTPUT_PATH  => [
        hde->path_to( 'root', 'static', 'published' ),
    ],
    ERROR        => 'error.tt2',
    TIMER        => 0
});

my $outfile;

sub output {
 my $output = shift;
# Nothing shows up here
}

sub process {
 my $self = shift;
 my ($c, $outfile) = @_; # Not ignoring $c, so I can get debug stuff
                                         # and delete body

  # $outfile = 'Test1.0.0 /TOC.html';

 $c->log->debug("In sub process"); # does print
 $c->log->debug("outfile: $outfile");

 my $output = delete $c->res->{body};
 open my $write_fh, '>', $outfile
   or die "Cannot open $outfile for writing: $!\n";
 print $write_fh $output;
 close $write_fh or warn "Cannot close $outfile: $!\n";

 return $self->next::method(@_);
}
_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to