Re: [Templates] CHanging content type under Apache::Template.

[prev] [thread] [next] [Date index for 2004/04/26]

From: merlyn (Randal L. Schwartz)
Subject: Re: [Templates] CHanging content type under Apache::Template.
Date: 15:22 on 26 Apr 2004
>>>>> "Lo=EFc" =3D=3D Lo=EFc Paillotin <loic.paillotin@xxxxxxxxxx.xxx> writ=
es:

Lo=EFc> Hello,=20
Lo=EFc> First of all I'm very new to the template toolkit so please bear wi=
trh
Lo=EFc> me :)

Lo=EFc> I use Apache::Template in order to display the content of a directo=
ry=20
Lo=EFc> ( your standard <Location /blah>... </Location> stuff) but I need to
Lo=EFc> change the content-type of the response (switch from text/html to
Lo=EFc> text/vnd.wap.wml ...) Is that posssible?=20

I had a similar problem... so I created this:

    package Stonehenge::Template::Service::Apache;
    use base qw(Template::Service::Apache);

    sub params {
      ## use Stonehenge::Reload; goto &params if Stonehenge::Reload->reload=
_me;

      my $self =3D shift;
      my $params =3D $self->SUPER::params(@_);
      $params->{content_type} =3D sub {
        ## warn "Calling content_type with @_";
        Apache->request->content_type(@_);
      };
      return $params;
    }

    ## I hate cut-n-paste
    sub headers {
        my ($self, $r, $template, $content) =3D @_;
        my $headers =3D $self->{ SERVICE_HEADERS };
        my $all =3D $headers->{ all };

        ## just to comment this out ## $r->content_type('text/html');
        ## warn "content type when sending headers is ", $r->content_type();
        $r->headers_out->add('Last-Modified'  =3D> ht_time($template->modti=
me()))
            if $all or $headers->{ modified } and $template;
        $r->headers_out->add('Content-Length' =3D> length $$content)
            if $all or $headers->{ length };
        $r->headers_out->add('E-tag' =3D> sprintf q{"%s"}, md5_hex($$conten=
t))
            if $all or $headers->{ etag };
        $r->send_http_header;
    }

    1;

Note the comment... Apache::Template isn't pluggable enough, so I had
to cut-n-paste (evil) the entire headers method just to disable the setting
of content-type. :)

But now, from within my template code, I can say things like:

        [%
                IF content_type() =3D=3D "text/html";
                ...
                END;
        %]

to see if the mime phase thought this was html, and:

        [%
                content_type("text/xml");
        %]

to force it.

--=20
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@xxxxxxxxxx.xxx> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl trainin=
g!

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] CHanging content type under Apache::Template.
=?ISO-8859-1?Q?Lo=EFc?= Paillotin 13:09 on 26 Apr 2004

Re: [Templates] CHanging content type under Apache::Template.
=?ISO-8859-1?Q?Lo=EFc?= Paillotin 13:47 on 26 Apr 2004

Re: [Templates] CHanging content type under Apache::Template.
=?ISO-8859-1?Q?Lo=EFc?= Paillotin 07:52 on 27 Apr 2004

Re: [Templates] CHanging content type under Apache::Template.
merlyn (Randal L. Schwartz) 15:22 on 26 Apr 2004

[Templates] Re: Apache::Template 0.09 (was: changing content type)
merlyn (Randal L. Schwartz) 13:32 on 27 Apr 2004

Re: [Templates] Apache::Template 0.09
Geoffrey Young 21:38 on 27 Apr 2004

Re: [Templates] Apache::Template 0.09
merlyn (Randal L. Schwartz) 21:50 on 27 Apr 2004

Re: [Templates] Apache::Template 0.09
Geoffrey Young 22:07 on 27 Apr 2004

Generated at 08:56 on 15 Mar 2005 by mariachi v0.52