Ticket #7281: ticket_7281.patch

File ticket_7281.patch, 5.3 KB (added by d1ffuz0r, 2 years ago)

Improvements. Tag moved to builtins, tests and documentation.

  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index ea1dd02..1548ab1 100644
    a b class WithNode(Node): 
    480480        context.pop()
    481481        return output
    482482
     483
     484class DoctypeNode(Node):
     485    def __init__(self, type):
     486        self.type = type
     487        self.doctypes = {
     488            'html4strict': '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
     489            'html4trans': '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
     490            'html4frameset': '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
     491            'xhtml1strict': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
     492            'xhtml1trans': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
     493            'xhtml1frameset': 'o<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
     494            'html5': '<!DOCTYPE HTML>'
     495        }
     496
     497    def render(self, context):
     498        if self.type in self.doctypes:
     499            context['_doctype'] = self.type # Set context variable for possible use by other HTML output mechanisms.
     500            return self.doctypes[self.type]
     501        else:
     502            return ''
     503
     504
    483505@register.tag
    484506def autoescape(parser, token):
    485507    """
    def do_with(parser, token): 
    13581380    nodelist = parser.parse(('endwith',))
    13591381    parser.delete_first_token()
    13601382    return WithNode(None, None, nodelist, extra_context=extra_context)
     1383
     1384
     1385@register.tag
     1386def doctype(parser, token):
     1387    """
     1388    Creates the HTML DOCTYPE declaration based on given type.
     1389
     1390    Usage format::
     1391
     1392        {% doctype type %}
     1393
     1394    ``type`` is one of: html4strict, html4trans, html4frameset, xhtml1strict, xhtml1trans, xhtml1frameset, html5.
     1395
     1396    Examples:
     1397        * ``{% doctype html4strict %}`` will output the HTML 4.01 Strict doctype.
     1398        * ``{% doctype xhtml1trans %}`` will output the XHTML 1.0 Transitional doctype.
     1399    """
     1400    bits = list(token.split_contents())
     1401    tagname, bits = bits[0], bits[1:]
     1402    type = ''
     1403    # Type bit
     1404    if len(bits):
     1405        type = bits.pop()
     1406    else:
     1407        raise TemplateSyntaxError("No type given for %r tag" % tagname)
     1408    if len(bits) > 0:
     1409        raise TemplateSyntaxError("Incorrect format for %r tag" % tagname)
     1410    return DoctypeNode(type)
  • docs/ref/templates/builtins.txt

    diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
    index 07ac284..ca4003f 100644
    a b debug 
    191191Outputs a whole load of debugging information, including the current context
    192192and imported modules.
    193193
     194.. templatetag:: doctype
     195
     196doctype
     197^^^^^^^
     198
     199.. versionadded:: 1.5
     200
     201Creates the HTML DOCTYPE declaration based on given type.
     202
     203Usage format::
     204
     205    {% doctype type %}
     206
     207``type`` is one of: html4strict, html4trans, html4frameset, xhtml1strict, xhtml1trans, xhtml1frameset, html5.
     208
     209Examples:
     210
     211* ``{% doctype html4strict %}`` will output the HTML 4.01 Strict doctype.
     212* ``{% doctype xhtml1trans %}`` will output the XHTML 1.0 Transitional doctype.
     213
    194214.. templatetag:: extends
    195215
    196216extends
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index a150d1c..ab9c844 100644
    a b class Templates(unittest.TestCase): 
    16401640            'verbatim-tag04': ('{% verbatim %}{% verbatim %}{% endverbatim %}{% endverbatim %}', {}, template.TemplateSyntaxError),
    16411641            'verbatim-tag05': ('{% verbatim %}{% endverbatim %}{% verbatim %}{% endverbatim %}', {}, ''),
    16421642            'verbatim-tag06': ("{% verbatim special %}Don't {% endverbatim %} just yet{% endverbatim special %}", {}, "Don't {% endverbatim %} just yet"),
     1643
     1644
     1645            # Doctype template tag outputs
     1646            'doctype-taghtml4strict': ('{% doctype html4strict %}', {}, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'),
     1647            'doctype-taghtml4trans': ('{% doctype html4trans %}', {}, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'),
     1648            'doctype-taghtml4frameset': ('{% doctype html4frameset %}', {}, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'),
     1649            'doctype-tagxhtml1strict': ('{% doctype xhtml1strict %}', {}, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'),
     1650            'doctype-tagxhtml1trans': ('{% doctype xhtml1trans %}', {}, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'),
     1651            'doctype-tagxhtml1frameset': ('{% doctype xhtml1frameset %}', {}, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'),
     1652            'doctype-taghtml5': ('{% doctype html5 %}', {}, '<!DOCTYPE HTML>')
    16431653        }
    16441654        return tests
    16451655
Back to Top