mirror of
https://github.com/django-components/django-components.git
synced 2025-09-20 04:39:45 +00:00
feat: TagFormatter - Allow users to customize component template tags (#572)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
b89c09aa5f
commit
71d8679e8d
23 changed files with 1593 additions and 474 deletions
|
@ -4,6 +4,14 @@ from django.template import Context
|
|||
from django.template.base import FilterExpression, Parser
|
||||
|
||||
|
||||
class AggregateFilterExpression:
|
||||
def __init__(self, dict: Dict[str, FilterExpression]) -> None:
|
||||
self.dict = dict
|
||||
|
||||
|
||||
Expression = Union[FilterExpression, AggregateFilterExpression]
|
||||
|
||||
|
||||
def resolve_expression_as_identifier(
|
||||
context: Context,
|
||||
fexp: FilterExpression,
|
||||
|
@ -20,19 +28,22 @@ def resolve_expression_as_identifier(
|
|||
return resolved
|
||||
|
||||
|
||||
def safe_resolve_list(args: List[FilterExpression], context: Context) -> List:
|
||||
def safe_resolve_list(args: List[Expression], context: Context) -> List:
|
||||
return [safe_resolve(arg, context) for arg in args]
|
||||
|
||||
|
||||
def safe_resolve_dict(
|
||||
kwargs: Union[Mapping[str, FilterExpression], Dict[str, FilterExpression]],
|
||||
kwargs: Union[Mapping[str, Expression], Dict[str, Expression]],
|
||||
context: Context,
|
||||
) -> Dict:
|
||||
return {key: safe_resolve(kwarg, context) for key, kwarg in kwargs.items()}
|
||||
|
||||
|
||||
def safe_resolve(context_item: FilterExpression, context: Context) -> Any:
|
||||
def safe_resolve(context_item: Expression, context: Context) -> Any:
|
||||
"""Resolve FilterExpressions and Variables in context if possible. Return other items unchanged."""
|
||||
if isinstance(context_item, AggregateFilterExpression):
|
||||
return safe_resolve_dict(context_item.dict, context)
|
||||
|
||||
return context_item.resolve(context) if hasattr(context_item, "resolve") else context_item
|
||||
|
||||
|
||||
|
@ -42,5 +53,11 @@ def resolve_string(
|
|||
context: Optional[Mapping[str, Any]] = None,
|
||||
) -> str:
|
||||
parser = parser or Parser([])
|
||||
context = context or {}
|
||||
context = Context(context or {})
|
||||
return parser.compile_filter(s).resolve(context)
|
||||
|
||||
|
||||
def is_aggregate_key(key: str) -> bool:
|
||||
# NOTE: If we get a key that starts with `:`, like `:class`, we do not split it.
|
||||
# This syntax is used by Vue and AlpineJS.
|
||||
return ":" in key and not key.startswith(":")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue