envbase
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| EnvironmentBase jinjarope.envbase Methods shared by the environments of all backends (see `jinjarope.engine`). |
🛈 DocStrings¶
Engine-independent part of the jinjarope environments (all backends).
EnvironmentBase
¶
Methods shared by the environments of all backends (see jinjarope.engine).
Subclasses provide filters, tests, globals, loader and the render methods.
Source code in src/jinjarope/envbase.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
add_template
¶
Add a new template during runtime.
This function adds a new template to the environment during runtime by creating a new DictLoader and injecting it into the existing loaders. This allows rendering templates that were not defined when the environment was initialized.
Use case
This function is particularly useful for situations where a template needs to be rendered dynamically, such as when rendering templates within other templates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | PathLike[str]
|
File to add as a template |
required |
Source code in src/jinjarope/envbase.py
add_template_path
¶
Add a new template path during runtime.
This function adds a new template path to the environment during runtime by appending a new FileSystemLoader to the existing loaders. This allows the environment to find templates in additional locations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Template search path(s) to add |
()
|
Source code in src/jinjarope/envbase.py
evaluate
¶
evaluate(code: str, context: dict[str, Any] | None = None) -> Any
Evaluate python code and return the caught stdout + return value of last line.
The code runs in a copy of the environment's globals (updated with
context), so its assignments don't leak into the environment.
Output of print() calls is captured without swapping sys.stdout
(writes to sys.stdout itself are not captured).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
The code to execute |
required |
context
|
dict[str, Any] | None
|
Additional globals for the execution |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The value of the last line if nothing was printed ( |
Any
|
otherwise the printed output followed by that value. |
Source code in src/jinjarope/envbase.py
get_source
¶
Return the source of a template of the loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_name
|
str
|
The template name |
required |
Raises:
| Type | Description |
|---|---|
TemplateNotFound
|
The loader doesn't know the template (or there is none) |
Source code in src/jinjarope/envbase.py
load_jinja_file
¶
load_jinja_file(
path: str | PathLike[str],
scope_prefix: str = "",
load_filters: bool = True,
load_tests: bool = True,
load_functions: bool = True,
load_config: bool = True,
load_loader: bool = True,
) -> None
Load the content of a JinjaFile and add it to the environment.
This function reads a JinjaFile and adds its filters, tests, functions, and configuration to the current environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
The path to the JinjaFile |
required |
scope_prefix
|
str
|
Optional prefix to add to all tests / filters / functions |
''
|
load_filters
|
bool
|
Whether to load filters from the JinjaFile |
True
|
load_tests
|
bool
|
Whether to load tests from the JinjaFile |
True
|
load_functions
|
bool
|
Whether to load functions from the JinjaFile |
True
|
load_config
|
bool
|
Whether to load the environment config from the JinjaFile (if
it has a |
True
|
load_loader
|
bool
|
Whether to load the Loader from the JinjaFile |
True
|
Source code in src/jinjarope/envbase.py
render_condition
¶
render_condition(
string: str, variables: dict[str, Any] | None = None, **kwargs: Any
) -> bool
Render a template condition.
This function renders a template string and evaluates its result as a boolean. It returns True if the result is truthy (not None, False, or an empty string), otherwise False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
String to evaluate for True-ishness |
required |
variables
|
dict[str, Any] | None
|
Extra variables for the rendering |
None
|
kwargs
|
Any
|
Further extra variables for rendering |
{}
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the rendered string is truthy, False otherwise. |
Source code in src/jinjarope/envbase.py
render_file
¶
render_file(
file: str | PathLike[str], variables: dict[str, Any] | None = None, **kwargs: Any
) -> str
Helper to directly render a template from filesystem.
This function renders a template file directly from the filesystem using the current environment's configuration and globals.
Info
The file content is cached, which is generally acceptable for common use cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | PathLike[str]
|
Template file to load |
required |
variables
|
dict[str, Any] | None
|
Extra variables for the rendering |
None
|
kwargs
|
Any
|
Further extra variables for rendering |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The rendered string. |
Source code in src/jinjarope/envbase.py
setup_loader
¶
setup_loader(
dir_paths: list[str] | None = None,
module_paths: list[str] | None = None,
static: dict[str, str] | None = None,
fsspec_paths: bool = True,
) -> None
Set the loader for the environment.
This function sets the loader for the environment based on
the provided parameters. It uses the jinjarope.get_loader
function to create a suitable loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_paths
|
list[str] | None
|
List of directory paths to search for templates |
None
|
module_paths
|
list[str] | None
|
List of module paths to search for templates |
None
|
static
|
dict[str, str] | None
|
Dictionary of static files to include in the loader |
None
|
fsspec_paths
|
bool
|
Whether to add a loader taking any local path or fsspec URL
as template name. With it, templates can include any file or URL,
outside of |
True
|
Source code in src/jinjarope/envbase.py
with_globals
¶
Context manager to temporarily set globals for the environment.
This context manager allows temporarily overriding the environment's globals with the provided values. The previous values are restored on exit, also when the block raises.
The globals dict is changed in place (templates loaded before see the values too), so this is not safe while other threads or tasks render with the same environment: pass render variables there instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
Globals to set |
{}
|