Hi there, I was looking at the modeltags feature in the new webhelpers. Unfortunately it only renders tags in XHTML mode. Since I prefer not to use that (see http://webkit.org/blog/68/understanding-html-xml-and-xhtml/) i've added a flag to the webhelpers.html.HTMLBuilder to render it as HTML.
I'm not entirely happy with how I did it, but it does work. Perhaps the cleanest way is to subclass the HTMLBuilder in an XHTMLBuilder which renders the tags as xhtml tags and an HTML(4)Builder which renders the tags as html 4 strict tags. Regards, Michael van Tellingen (mvt) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-devel" group. To post to this group, send email to pylons-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en -~----------~----~----~----~------~----~------~--~---
diff -r 5b974015c354 webhelpers/html.py --- a/webhelpers/html.py Thu Apr 24 10:59:10 2008 -0700 +++ b/webhelpers/html.py Mon Apr 28 13:13:17 2008 +0200 @@ -41,17 +41,21 @@ """Represents an unfinished or empty tag.""" - def __init__(self, tag): + def __init__(self, tag, doctype="XHTML"): """Initialize with the tag name.""" self._tag = tag + self._doctype = doctype def __call__(self, *args, **kw): """Create the tag with the arguments passed in.""" - return make_tag(self._tag, *args, **kw) + return make_tag(self._tag, self._doctype, *args, **kw) def __str__(self): """Return a literal representation.""" - return literal('<%s />' % self._tag) + if self._doctype == "HTML": + return literal('<%s>' % self._tag) + else: + return literal('<%s />' % self._tag) def __html__(self): """Return the HTML escaped tag.""" @@ -90,18 +94,28 @@ comment = UnfinishedComment() literal = UnfinishedLiteral() - + def __init__(self): + self._doctype = "XHTML" + def __getattr__(self, attr): """Generate the tag for the given attribute name.""" if attr.startswith('_'): raise AttributeError - result = self.__dict__[attr] = UnfinishedTag(attr.lower()) + result = self.__dict__[attr] = UnfinishedTag(attr.lower(), + self._doctype) return result def __call__(self, *args): """Join raw HTML and HTML escape it.""" return ''.join([escape(x) for x in args]) + def set_doctype(self, value): + if value not in ('HTML', 'XHTML'): + raise ValueError("Unsupported doctype") + if value != self._doctype: + self.__dict__ = dict() + self._doctype = value + doctype = property(fset=set_doctype) def attrEncode(v): """Parse out attributes that begin with '_'.""" @@ -111,7 +125,7 @@ return v -def make_tag(tag, *args, **kw): +def make_tag(tag, doctype, *args, **kw): if kw.has_key("c"): assert not args, "The special 'c' keyword argument cannot be used "\ "in conjunction with non-keyword arguments" @@ -121,7 +135,10 @@ for attr, value in sorted(kw.iteritems()) if value is not None] if not args and emptyTags.has_key(tag) and closed: - substr = '<%s%s />' + if doctype == "HTML": + substr = '<%s%s>' + else: + substr = '<%s%s />' if blockTags.has_key(tag): return literal(substr % (tag, "".join(htmlArgs))) else: