mirror of
https://github.com/django-components/django-components.git
synced 2025-08-10 17:28:00 +00:00
feat: paths as objects + user-provided Media cls + handle static (#526)
Co-authored-by: Emil Stenström <emil@emilstenstrom.se>
This commit is contained in:
parent
1d0d960211
commit
3c5a7ad823
10 changed files with 1106 additions and 146 deletions
|
@ -0,0 +1,33 @@
|
|||
from typing import Any, Dict
|
||||
|
||||
from django.templatetags.static import static
|
||||
from django.utils.html import format_html, html_safe
|
||||
|
||||
from django_components import component
|
||||
|
||||
|
||||
# Format as mentioned in https://github.com/EmilStenstrom/django-components/issues/522#issuecomment-2173577094
|
||||
@html_safe
|
||||
class PathObj:
|
||||
def __init__(self, static_path: str) -> None:
|
||||
self.static_path = static_path
|
||||
self.throw_on_calling_str = True
|
||||
|
||||
def __str__(self):
|
||||
# This error will notify us when we've hit __str__ when we shouldn't have
|
||||
if self.throw_on_calling_str:
|
||||
raise RuntimeError("__str__ method of 'relative_file_pathobj_component' was triggered when not allow to")
|
||||
|
||||
return format_html('<script type="module" src="{}"></script>', static(self.static_path))
|
||||
|
||||
|
||||
@component.register("relative_file_pathobj_component")
|
||||
class RelativeFileWithPathObjComponent(component.Component):
|
||||
template_name = "relative_file_pathobj.html"
|
||||
|
||||
class Media:
|
||||
js = PathObj("relative_file_pathobj.js")
|
||||
css = PathObj("relative_file_pathobj.css")
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
Loading…
Add table
Add a link
Reference in a new issue