-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Description
Documentation
The template can then be combined with functions that operate on the template’s structure to produce a str or a string-like result. For example, sanitizing input:
template = t"<p>{evil}</p>" assert html(template) == "<p><script>alert('evil')</script></p>"
As another example, generating HTML attributes from data:
template = t"<img {attributes}>" assert html(template) == '<img src="/api/flow.js?q=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2Fshrubbery.jpg" alt="looks nice" />'
Compared to using an f-string, the html function has access to template attributes containing the original information: static strings, interpolations, and values from the original scope. Unlike existing templating approaches, t-strings build from the well-known f-string syntax and rules. Template systems thus benefit from Python tooling as they are much closer to the Python language, syntax, scoping, and more.
In PEP-750, the author assumed that you had defined the function html
:
For example, imagine we want to generate some HTML. Using template strings, we can define an html() function that allows us to automatically sanitize content:
template = t"<p>{evil}</p>" assert html(template) == "<p><script>alert('evil')</script></p>"
Likewise, our hypothetical html() function can make it easy for developers to add attributes to HTML elements using a dictionary:
template = t"<img {attributes} />" assert html(template) == '<img src="/api/flow.js?q=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2Fshrubbery.jpg" alt="looks nice" />'
However, in the document, the operation "define" was gone, so that some users will mistakenly think that there is a function html
that has been defined.
I think that it is better to explain that the function html
should be defined by user, like this:
The template can then be combined with functions that operate on the template’s structure to produce a str or a string-like result. For example, sanitizing input (assuming that you have defined a function
html
that can deal with the input to html):
Metadata
Metadata
Assignees
Labels
Projects
Status