A compiled template. We compile into Python from the given template_string. You can generate the template from variables with generate().
| 250 | |
| 251 | |
| 252 | class Template(object): |
| 253 | """A compiled template. |
| 254 | |
| 255 | We compile into Python from the given template_string. You can generate |
| 256 | the template from variables with generate(). |
| 257 | """ |
| 258 | |
| 259 | # note that the constructor's signature is not extracted with |
| 260 | # autodoc because _UNSET looks like garbage. When changing |
| 261 | # this signature update website/sphinx/template.rst too. |
| 262 | def __init__( |
| 263 | self, |
| 264 | template_string: Union[str, bytes], |
| 265 | name: str = "<string>", |
| 266 | loader: Optional["BaseLoader"] = None, |
| 267 | compress_whitespace: Union[bool, _UnsetMarker] = _UNSET, |
| 268 | autoescape: Optional[Union[str, _UnsetMarker]] = _UNSET, |
| 269 | whitespace: Optional[str] = None, |
| 270 | ) -> None: |
| 271 | """Construct a Template. |
| 272 | |
| 273 | :arg str template_string: the contents of the template file. |
| 274 | :arg str name: the filename from which the template was loaded |
| 275 | (used for error message). |
| 276 | :arg tornado.template.BaseLoader loader: the `~tornado.template.BaseLoader` responsible |
| 277 | for this template, used to resolve ``{% include %}`` and ``{% extend %}`` directives. |
| 278 | :arg bool compress_whitespace: Deprecated since Tornado 4.3. |
| 279 | Equivalent to ``whitespace="single"`` if true and |
| 280 | ``whitespace="all"`` if false. |
| 281 | :arg str autoescape: The name of a function in the template |
| 282 | namespace, or ``None`` to disable escaping by default. |
| 283 | :arg str whitespace: A string specifying treatment of whitespace; |
| 284 | see `filter_whitespace` for options. |
| 285 | |
| 286 | .. versionchanged:: 4.3 |
| 287 | Added ``whitespace`` parameter; deprecated ``compress_whitespace``. |
| 288 | """ |
| 289 | self.name = escape.native_str(name) |
| 290 | |
| 291 | if compress_whitespace is not _UNSET: |
| 292 | # Convert deprecated compress_whitespace (bool) to whitespace (str). |
| 293 | if whitespace is not None: |
| 294 | raise Exception("cannot set both whitespace and compress_whitespace") |
| 295 | whitespace = "single" if compress_whitespace else "all" |
| 296 | if whitespace is None: |
| 297 | if loader and loader.whitespace: |
| 298 | whitespace = loader.whitespace |
| 299 | else: |
| 300 | # Whitespace defaults by filename. |
| 301 | if name.endswith(".html") or name.endswith(".js"): |
| 302 | whitespace = "single" |
| 303 | else: |
| 304 | whitespace = "all" |
| 305 | # Validate the whitespace setting. |
| 306 | assert whitespace is not None |
| 307 | filter_whitespace(whitespace, "") |
| 308 | |
| 309 | if not isinstance(autoescape, _UnsetMarker): |
no outgoing calls