Loads a template. This method looks up the template in the cache or loads one by calling :meth:`get_source`. Subclasses should not override this method as loaders working on collections of other loaders (such as :class:`PrefixLoader` or :class:`ChoiceLoader`) will n
(
self,
environment: "Environment",
name: str,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
)
| 106 | |
| 107 | @internalcode |
| 108 | def load( |
| 109 | self, |
| 110 | environment: "Environment", |
| 111 | name: str, |
| 112 | globals: t.Optional[t.MutableMapping[str, t.Any]] = None, |
| 113 | ) -> "Template": |
| 114 | """Loads a template. This method looks up the template in the cache |
| 115 | or loads one by calling :meth:`get_source`. Subclasses should not |
| 116 | override this method as loaders working on collections of other |
| 117 | loaders (such as :class:`PrefixLoader` or :class:`ChoiceLoader`) |
| 118 | will not call this method but `get_source` directly. |
| 119 | """ |
| 120 | code = None |
| 121 | if globals is None: |
| 122 | globals = {} |
| 123 | |
| 124 | # first we try to get the source for this template together |
| 125 | # with the filename and the uptodate function. |
| 126 | source, filename, uptodate = self.get_source(environment, name) |
| 127 | |
| 128 | # try to load the code from the bytecode cache if there is a |
| 129 | # bytecode cache configured. |
| 130 | bcc = environment.bytecode_cache |
| 131 | if bcc is not None: |
| 132 | bucket = bcc.get_bucket(environment, name, filename, source) |
| 133 | code = bucket.code |
| 134 | |
| 135 | # if we don't have code so far (not cached, no longer up to |
| 136 | # date) etc. we compile the template |
| 137 | if code is None: |
| 138 | code = environment.compile(source, name, filename) |
| 139 | |
| 140 | # if the bytecode cache is available and the bucket doesn't |
| 141 | # have a code so far, we give the bucket the new code and put |
| 142 | # it back to the bytecode cache. |
| 143 | if bcc is not None and bucket.code is None: |
| 144 | bucket.code = code |
| 145 | bcc.set_bucket(bucket) |
| 146 | |
| 147 | return environment.template_class.from_code( |
| 148 | environment, code, globals, uptodate |
| 149 | ) |
| 150 | |
| 151 | |
| 152 | class FileSystemLoader(BaseLoader): |
no test coverage detected