[Checkins] SVN: z3c.pt/trunk/ - Set Content-Type header, for backwards compatibility with
Sidnei da Silva
sidnei at enfoldsystems.com
Mon Apr 6 23:39:17 EDT 2009
Log message for revision 98962:
- Set Content-Type header, for backwards compatibility with
zope.app.pagetemplate. [sidnei]
Changed:
U z3c.pt/trunk/CHANGES.txt
U z3c.pt/trunk/src/z3c/pt/README.txt
U z3c.pt/trunk/src/z3c/pt/pagetemplate.py
-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt 2009-04-07 03:33:50 UTC (rev 98961)
+++ z3c.pt/trunk/CHANGES.txt 2009-04-07 03:39:17 UTC (rev 98962)
@@ -1,6 +1,11 @@
Changelog
---------
+In the next release
+
+- Set Content-Type header, for backwards compatibility with
+ zope.app.pagetemplate. [sidnei]
+
1.0b14 (2009/03/31)
~~~~~~~~~~~~~~~~~~~
Modified: z3c.pt/trunk/src/z3c/pt/README.txt
===================================================================
--- z3c.pt/trunk/src/z3c/pt/README.txt 2009-04-07 03:33:50 UTC (rev 98961)
+++ z3c.pt/trunk/src/z3c/pt/README.txt 2009-04-07 03:39:17 UTC (rev 98962)
@@ -22,6 +22,7 @@
Hello World!
</div>
+
The ``PageTemplateFile`` class is initialized with an absolute
path to a template file on disk.
@@ -37,6 +38,68 @@
>>> template_file.filename.startswith(os.sep)
True
+If a ``content_type`` is not informed and one is not present in the
+request, it will be set to 'text/html'.
+
+ >>> class Response(object):
+ ... def __init__(self):
+ ... self.headers = {}
+ ... self.getHeader = self.headers.get
+ ... self.setHeader = self.headers.__setitem__
+
+ >>> class Request(object):
+ ... def __init__(self):
+ ... self.response = Response()
+
+ >>> template_file = PageTemplateFile(path+'/helloworld.pt')
+ >>> request = Request()
+ >>> print request.response.getHeader('Content-Type')
+ None
+
+ >>> template = template_file.bind(None, request=request)
+ >>> print template()
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ Hello World!
+ </div>
+
+ >>> print request.response.getHeader('Content-Type')
+ text/html
+
+If a ``content_type`` is present in the request, then we don't override it.
+
+ >>> request = Request()
+ >>> request.response.setHeader('Content-Type', 'text/xml')
+ >>> print request.response.getHeader('Content-Type')
+ text/xml
+
+ >>> template = template_file.bind(None, request=request)
+ >>> print template()
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ Hello World!
+ </div>
+
+ >>> print request.response.getHeader('Content-Type')
+ text/xml
+
+A ``content_type`` can be also set at instantiation time, and it will
+be respected.
+
+ >>> template_file = PageTemplateFile(path+'/helloworld.pt',
+ ... content_type='application/rdf+xml')
+
+ >>> request = Request()
+ >>> print request.response.getHeader('Content-Type')
+ None
+
+ >>> template = template_file.bind(None, request=request)
+ >>> print template()
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ Hello World!
+ </div>
+
+ >>> print request.response.getHeader('Content-Type')
+ application/rdf+xml
+
Both may be used as class attributes (properties).
>>> class MyClass(object):
Modified: z3c.pt/trunk/src/z3c/pt/pagetemplate.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/pagetemplate.py 2009-04-07 03:33:50 UTC (rev 98961)
+++ z3c.pt/trunk/src/z3c/pt/pagetemplate.py 2009-04-07 03:39:17 UTC (rev 98962)
@@ -76,22 +76,29 @@
return False
class BaseTemplate(template.PageTemplate):
+ content_type = None
default_parser = language.Parser()
version = 2
def bind(self, ob, request=None, macro=None, global_scope=True):
- def render(target_language=None, **kwargs):
+ def render(target_language=None, request=request, **kwargs):
context = self._pt_get_context(ob, request, kwargs)
+ request = request or context.get('request')
if target_language is None:
try:
- target_language = i18n.negotiate(
- request or context.get('request'))
+ target_language = i18n.negotiate(request)
except:
target_language = None
context['target_language'] = target_language
context["econtext"] = utils.econtext(context)
+ if request is not None and not isinstance(request, basestring):
+ content_type = self.content_type or 'text/html'
+ if not request.response.getHeader("Content-Type"):
+ request.response.setHeader(
+ "Content-Type", content_type)
+
if macro is None:
return self.render(**context)
else:
@@ -142,6 +149,10 @@
template.PageTemplateFile.__init__(
self, filename, **kwargs)
+ # Set content-type last, so that we can override whatever was
+ # magically sniffed from the source template.
+ self.content_type = content_type
+
class PageTemplate(BaseTemplate):
"""Page Templates using TAL, TALES, and METAL.
More information about the Checkins
mailing list