mirror of
https://github.com/django-components/django-components.git
synced 2025-09-11 16:36:17 +00:00
Use built-in django form assets instead of custom logic.
This commit is contained in:
parent
379c35d0b1
commit
096057b33d
5 changed files with 24 additions and 25 deletions
|
@ -1,30 +1,22 @@
|
|||
from django.template.loader import render_to_string
|
||||
from .component_registry import ComponentRegistry, AlreadyRegistered, NotRegistered # NOQA
|
||||
from django.forms.widgets import MediaDefiningClass
|
||||
|
||||
class Component(object):
|
||||
CSS_TEMPLATE = '<link href="{}" type="text/css" media="{}" rel="stylesheet" />'
|
||||
JS_TEMPLATE = '<script type="text/javascript" src="{}"></script>'
|
||||
|
||||
class Component(metaclass=MediaDefiningClass):
|
||||
def context(self):
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
def render_dependencies(cls):
|
||||
out = []
|
||||
def template(self, context):
|
||||
return ""
|
||||
|
||||
for css_media, css_path in cls.Media.css.items():
|
||||
out.append(cls.CSS_TEMPLATE.format(css_path, css_media))
|
||||
|
||||
for js_path in cls.Media.js:
|
||||
out.append(cls.JS_TEMPLATE.format(js_path))
|
||||
|
||||
return "\n".join(out)
|
||||
def render_dependencies(self):
|
||||
return self.media.render()
|
||||
|
||||
def render(self, *args, **kwargs):
|
||||
return render_to_string(self.Media.template, self.context(*args, **kwargs))
|
||||
context = self.context(*args, **kwargs)
|
||||
return render_to_string(self.template(context), context)
|
||||
|
||||
class Media:
|
||||
template = None
|
||||
css = {}
|
||||
js = []
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue