Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

creating a Catalyst View component

by holli (Abbot)
on Feb 26, 2006 at 17:32 UTC ( [id://532896]=perlquestion: print w/replies, xml ) Need Help??

holli has asked for the wisdom of the Perl Monks concerning the following question:

Brethren,

I am making Catalyst baby steps. During that struggle I tried to create a custom View component and set up the following code:
package ctest1::View::PDF; use strict; use base 'Catalyst::View'; sub process { print "Content-type: text/plain\n\n"; print "This is a test\n"; return 1; } 1;
The good new with this is, that I see the output of it. The bad news is, there is more than that:
Content-type: text/plain This is a test HTTP/1.0 200 OK Connection: keep-alive Date: Sun, 26 Feb 2006 17:28:38 GMT Set-Cookie: ctest1_session=1bcd798b2ab2d6805ccbc652c6341e39a97e9ef8; p +ath=/; expires=Sun, 26-Feb-2006 19:28:38 GMT Status: 200 X-Catalyst: 5.65
Obviously I am missing something, because I expected the output to be just:
This is a test
What am I doing wrong?


holli, /regexed monk/

Replies are listed 'Best First'.
Re: creating a Catalyst View component
by rhesa (Vicar) on Feb 26, 2006 at 17:47 UTC
    I'm not a Catalyst user at all, but I'm pretty sure you're not supposed to print anywhere in your code. Instead, you should be looking at
    • How to set custom outgoing headers
    • How to pass content around
    As far as I can see, you should be doing something like $c->response->content('Hello World');
    I assume there's something to set the content-type too, along the lines of $c->response->headers->header( 'Content-Type' => 'text/plain' );

    Remember that you're working with an MVC framework. The View's sole responsibility is to transform data from one format to another. It's the Controller's job to actually send that data to the recipient.

Re: creating a Catalyst View component
by shenme (Priest) on Feb 26, 2006 at 18:35 UTC
    As rhesa mentioned, you don't do output directly from your view. Rather you give the output generated by your view back to the Catalyst. (In this way, Catalyst is like CGI::Appl and several other MVC frameworks)

    While this interface seems cumbersome and adds a little overhead, it does allow for some nice capabilities, like centralized error processing, wrappers, or post-processing (think XSL transformations). I'm sure other people have really good examples of capabilities.

    Anyway, you want to be looking in the documentation for how to use $c->response->body (the shorthand reference you'll often see is $c->res->body . If you look into other view code sources, you will that view actions can end with code like:

    $c->response->body($output); return 1;
    where previously generated output in $output is given to Catalyst and the view routine simply returns. You replace or add to the output body contents using the API. When you return from the view component, Catalyst (or other components in your application) may examine or modify the contents of the body. Then Catalyst will output the header, then the body.

    You will also want to check out things like $c->response->status and $c->response->content_type to see how to interact with Catalyst to specify the other HTTP output data.

      Very well written and very helpful. Thank you.


      holli, /regexed monk/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://532896]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2024-04-24 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found