Construct a Template. :arg str template_string: the contents of the template file. :arg str name: the filename from which the template was loaded (used for error message). :arg tornado.template.BaseLoader loader: the `~tornado.template.BaseLoader` responsible
(
self,
template_string: Union[str, bytes],
name: str = "<string>",
loader: Optional["BaseLoader"] = None,
compress_whitespace: Union[bool, _UnsetMarker] = _UNSET,
autoescape: Optional[Union[str, _UnsetMarker]] = _UNSET,
whitespace: Optional[str] = None,
)
| 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): |
| 310 | self.autoescape = autoescape # type: Optional[str] |
| 311 | elif loader: |
| 312 | self.autoescape = loader.autoescape |
| 313 | else: |
| 314 | self.autoescape = _DEFAULT_AUTOESCAPE |
| 315 | |
| 316 | self.namespace = loader.namespace if loader else {} |
| 317 | reader = _TemplateReader(name, escape.native_str(template_string), whitespace) |
| 318 | self.file = _File(self, _parse(reader, self)) |
| 319 | self.code = self._generate_python(loader) |
nothing calls this directly
no test coverage detected