Re: [Templates] CHanging content type under Apache::Template.
[prev]
[thread]
[next]
[Date index for 2004/04/26]
>>>>> "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 ¶ms 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
 |
 |
Re: [Templates] CHanging content type under Apache::Template.
merlyn (Randal L. Schwartz) 15:22 on 26 Apr 2004
|