| 68 | |
| 69 | class DoctypeNode(template.Node): |
| 70 | def __init__(self, type): |
| 71 | self.type = type |
| 72 | self.doctypes = { |
| 73 | 'html4strict': u'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', |
| 74 | 'html4trans': u'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', |
| 75 | 'html4frameset': u'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">', |
| 76 | 'xhtml1strict': u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', |
| 77 | 'xhtml1trans': u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', |
| 78 | 'xhtml1frameset': u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">', |
| 79 | } |
| 80 | |
| 81 | def render(self, context): |
| 82 | if self.doctypes.has_key(self.type): |
| 83 | context['_doctype'] = self.type # Set context variable for possible use by other HTML output mechanisms. |
| 84 | return self.doctypes[self.type] |
| 85 | else: |
| 86 | return '' |
| 87 | |
| 88 | #@register.tag |
| 89 | def doctype(parser, token): |
| 90 | """ |
| 91 | Creates the HTML DOCTYPE declaration based on given type. |
| 92 | |
| 93 | Usage format:: |
| 94 | |
| 95 | {% doctype type %} |
| 96 | |
| 97 | ``type`` is one of: html4strict, html4trans, html4frameset, xhtml1strict, xhtml1trans, xhtml1frameset. |
| 98 | |
| 99 | Examples: |
| 100 | * ``{% doctype html4strict %}`` will output the HTML 4.01 Strict doctype. |
| 101 | * ``{% doctype xhtml1trans %}`` will output the XHTML 1.0 Transitional doctype. |
| 102 | """ |
| 103 | bits = list(token.split_contents()) |
| 104 | tagname, bits = bits[0], bits[1:] |
| 105 | type = '' |
| 106 | # Type bit |
| 107 | if len(bits): |
| 108 | type = bits.pop() |
| 109 | else: |
| 110 | raise template.TemplateSyntaxError("No type given for %r tag" % tagname) |
| 111 | if len(bits) > 0: |
| 112 | raise template.TemplateSyntaxError("Incorrect format for %r tag" % tagname) |
| 113 | return DoctypeNode(type) |
| 114 | doctype = register.tag(doctype) |