[Zope] How to set HTTP Headers with TAL?

Wade Leftwich wade@lightlink.com
Mon, 2 Sep 2002 07:18:22 -0400


On Monday 02 September 2002 06:50, mmartini@web.de wrote:
> Hi,
>
> does anybody know how to set HTTP headers with TAL (if possible).
>
> I want to do HTTP caching with Apache as explained here:
> http://www.zope.org/Members/rbeer/caching .
>
> These the lines in DTML:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <dtml-call "RESPONSE.setHeader('Expires',
> _.DateTime(_.DateTime().timeTime() + 3600).toZone('GMT').rfc822())">
> <dtml-call "RESPONSE.setHeader('Last-Modified',
> bobobase_modification_time().toZone('GMT').rfc822())">
> <HTML>
> ...
>
> Thank you for your help.
>
> Mitja

In TAL:

<span tal:define="nil python:request.RESPONSE.setHeader('Expires', (DateTime() 
+ 1.0/24).toZone('GMT').rfc822());
                  nil python:request.RESPONSE.setHeader('Last-Modified;, 
here.bobobase_modification_time().toZone('GMT').rfc822())" />

But you might consider moving all that into a python script that goes like 
this:

### script setheaders
response = container.REQUEST.RESPONSE

response.setHeader('Expires', (DateTime() + 1.0/24).toZone('GMT').rfc822())

response.setHeader('Last-Modified;, 
context.bobobase_modification_time().toZone('GMT').rfc822()) 
###

Then in your page template you can just say
<span tal:define="nil here/setheaders" />

For some good examples of how to translate various DTML idioms into TAL, see 
http://www.zope.org/Members/peterbe/DTML2ZPT

-- Wade Leftwich
Ithaca, NY US