Similar to str.format, but pass all arguments through conditional_escape(), and call mark_safe() on the result. This function should be used instead of str.format or % interpolation to build up small HTML fragments.
(format_string, *args, **kwargs)
| 133 | |
| 134 | |
| 135 | def format_html(format_string, *args, **kwargs): |
| 136 | """ |
| 137 | Similar to str.format, but pass all arguments through conditional_escape(), |
| 138 | and call mark_safe() on the result. This function should be used instead |
| 139 | of str.format or % interpolation to build up small HTML fragments. |
| 140 | """ |
| 141 | if not (args or kwargs): |
| 142 | raise TypeError("args or kwargs must be provided.") |
| 143 | args_safe = map(conditional_escape, args) |
| 144 | kwargs_safe = {k: conditional_escape(v) for (k, v) in kwargs.items()} |
| 145 | return mark_safe(format_string.format(*args_safe, **kwargs_safe)) |
| 146 | |
| 147 | |
| 148 | def format_html_join(sep, format_string, args_generator): |