>>>>> "Lo�c" == Lo�c Paillotin <[EMAIL PROTECTED]> writes:

Lo�c> Hello, 
Lo�c> First of all I'm very new to the template toolkit so please bear witrh
Lo�c> me :)

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

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 = shift;
      my $params = $self->SUPER::params(@_);
      $params->{content_type} = sub {
        ## warn "Calling content_type with @_";
        Apache->request->content_type(@_);
      };
      return $params;
    }

    ## I hate cut-n-paste
    sub headers {
        my ($self, $r, $template, $content) = @_;
        my $headers = $self->{ SERVICE_HEADERS };
        my $all = $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'  => ht_time($template->modtime()))
            if $all or $headers->{ modified } and $template;
        $r->headers_out->add('Content-Length' => length $$content)
            if $all or $headers->{ length };
        $r->headers_out->add('E-tag' => sprintf q{"%s"}, md5_hex($$content))
            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() == "text/html";
                ...
                END;
        %]

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

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

to force it.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <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 training!

_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to