Coverage for fastblocks/adapters/templates/registration.py: 83%
47 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-28 18:13 -0700
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-28 18:13 -0700
1"""Template filter registration system for FastBlocks adapters."""
3from contextlib import suppress
4from typing import Any
6from acb.depends import depends
8from .filters import FASTBLOCKS_FILTERS
11def register_fastblocks_filters(template_env: Any) -> None:
12 """Register all FastBlocks adapter filters with a Jinja2 environment.
14 Args:
15 template_env: Jinja2 Environment instance
16 """
17 for filter_name, filter_func in FASTBLOCKS_FILTERS.items():
18 template_env.filters[filter_name] = filter_func
21def register_async_fastblocks_filters(template_env: Any) -> None:
22 """Register async versions of FastBlocks adapter filters.
24 Args:
25 template_env: Jinja2 AsyncEnvironment instance
26 """
27 from .async_filters import FASTBLOCKS_ASYNC_FILTERS
29 # Register both sync and async filters
30 register_fastblocks_filters(template_env)
32 # Add async-specific filters
33 for filter_name, filter_func in FASTBLOCKS_ASYNC_FILTERS.items():
34 template_env.filters[filter_name] = filter_func
37def get_global_template_context() -> dict[str, Any]:
38 """Get global template context with adapter instances.
40 Returns:
41 Dict of global template variables
42 """
43 context = {}
45 # Add adapter instances to global context
46 with suppress(Exception):
47 images = depends.get("images")
48 if images:
49 context["images_adapter"] = images
51 with suppress(Exception):
52 styles = depends.get("styles")
53 if styles:
54 context["styles_adapter"] = styles
56 with suppress(Exception):
57 icons = depends.get("icons")
58 if icons:
59 context["icons_adapter"] = icons
61 with suppress(Exception):
62 fonts = depends.get("fonts")
63 if fonts:
64 context["fonts_adapter"] = fonts
66 return context
69def register_template_globals(template_env: Any) -> None:
70 """Register global template variables and functions.
72 Args:
73 template_env: Jinja2 Environment instance
74 """
75 globals_dict = get_global_template_context()
76 template_env.globals.update(globals_dict)
79def setup_fastblocks_template_environment(
80 template_env: Any, async_mode: bool = False
81) -> None:
82 """Complete setup of FastBlocks template environment.
84 Args:
85 template_env: Jinja2 Environment instance
86 async_mode: Whether to register async filters
87 """
88 # Register filters
89 if async_mode:
90 register_async_fastblocks_filters(template_env)
91 else:
92 register_fastblocks_filters(template_env)
94 # Register globals
95 register_template_globals(template_env)
97 # Configure template environment for FastBlocks
98 template_env.trim_blocks = True
99 template_env.lstrip_blocks = True
101 # Set custom delimiters if not already set
102 if not hasattr(template_env, "_fastblocks_delimiters_set"):
103 template_env.variable_start_string = "[["
104 template_env.variable_end_string = "]]"
105 template_env.block_start_string = "[%"
106 template_env.block_end_string = "%]"
107 template_env._fastblocks_delimiters_set = True