Currently the Genshi plugin ignores the fragment parameter to
render(). This is usually not a problem, but if one set the
default_doctype config option, then the doc_type is prepended to the
output even if the template does not contain any doctype.
I'm using Genshi with TurboGears and ToscaWidgets. For my TG project I
want to write templates as XHTML and render them as HTML. Thus I set
the default doctype to 'html-transitional'. But for my widgets I need
to return html fragments for ajax calls, and for them I don't want to
have the doctype present in the HTML, since injecting HTML into a DOM
tree with a doctype present gives me errors.
This patch makes the Genshi plugin ignore the default doctype when
fragment is set to True. With this patch Genshi will still render the
doctype if it's present in the template, but I don't think that should
be a big problem since one should not have a doctype in the template
when wanting to render html fragments. A better solution could also
made sure that the doctype is removed from the output if present in
the template when fragment is set to True (this is what Kid does), but
I couldn't find out how to do this. But I still think this patch makes
the plugin behave much better than before.
Index: plugin.py
===================================================================
--- plugin.py (revision 766)
+++ plugin.py (working copy)
@@ -107,7 +107,7 @@
def render(self, info, format=None, fragment=False,
template=None):
"""Render the template to a string using the provided
info."""
- kwargs = self._get_render_options(format=format)
+ kwargs = self._get_render_options(format=format,
fragment=fragment)
return self.transform(info, template).render(**kwargs)
def transform(self, info, template):
@@ -140,10 +140,10 @@
raise ConfigurationError('Unknown output format %r' %
format)
self.default_format = format
- def _get_render_options(self, format=None):
+ def _get_render_options(self, format=None, fragment=False):
kwargs = super(MarkupTemplateEnginePlugin,
self)._get_render_options(format)
- if self.default_doctype:
+ if self.default_doctype and not fragment:
kwargs['doctype'] = self.default_doctype
return kwargs
Kind regards
-- Dag
Am 06.11.2007 um 12:40 schrieb dbrattli:
> Currently the Genshi plugin ignores the fragment parameter to
> render(). This is usually not a problem, but if one set the
> default_doctype config option, then the doc_type is prepended to the
> output even if the template does not contain any doctype.
Thanks for the patch, I've applied it (plus unit test) in:
<http://genshi.edgewall.org/changeset/767>
Cheers,
Chris
--
Christopher Lenz
cmlenz at gmx.de
http://www.cmlenz.net/
Congratulations on now being a happy father :-) Best wishes to you and
your family from all of us Genshi users.
-- Dag