summaryrefslogtreecommitdiffstats
path: root/docs/formatting.rst
blob: c14f9170e9814f5ee6328b683f1ceeab169d29d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
.. currentmodule:: markupsafe

String Formatting
=================

The :class:`Markup` class can be used as a format string. Objects
formatted into a markup string will be escaped first.


Format Method
-------------

The ``format`` method extends the standard :meth:`str.format` behavior
to use an ``__html_format__`` method.

#.  If an object has an ``__html_format__`` method, it is called as a
    replacement for the ``__format__`` method. It is passed a format
    specifier if it's given. The method must return a string or
    :class:`Markup` instance.

#.  If an object has an ``__html__`` method, it is called. If a format
    specifier was passed and the class defined ``__html__`` but not
    ``__html_format__``, a ``ValueError`` is raised.

#.  Otherwise Python's default format behavior is used and the result
    is escaped.

For example, to implement a ``User`` that wraps its ``name`` in a
``span`` tag, and adds a link when using the ``"link"`` format
specifier:

.. code-block:: python

    class User(object):
        def __init__(self, id, name):
            self.id = id
            self.name = name

        def __html_format__(self, format_spec):
            if format_spec == "link":
                return Markup(
                    '<a href="/user/{}">{}</a>'
                ).format(self.id, self.__html__())
            elif format_spec:
                raise ValueError("Invalid format spec")
            return self.__html__()

        def __html__(self):
            return Markup(
                '<span class="user">{0}</span>'
            ).format(self.name)


.. code-block:: pycon

    >>> user = User(3, "<script>")
    >>> escape(user)
    Markup('<span class="user">&lt;script&gt;</span>')
    >>> Markup("<p>User: {user:link}").format(user=user)
    Markup('<p>User: <a href="/user/3"><span class="user">&lt;script&gt;</span></a>

See Python's docs on :ref:`format string syntax <python:formatstrings>`.


printf-style Formatting
-----------------------

Besides escaping, there's no special behavior involved with percent
formatting.

.. code-block:: pycon

    >>> user = User(3, "<script>")
    >>> Markup('<a href="/user/%d">%s</a>') % (user.id, user.name)
    Markup('<a href="/user/3">&lt;script&gt;</a>')

See Python's docs on :ref:`printf-style formatting <python:old-string-formatting>`.