mirror of
https://github.com/django-components/django-components.git
synced 2025-11-21 07:08:15 +00:00
Add component_dependencies template tag.
This commit is contained in:
parent
33b1930ed5
commit
176574a9cc
3 changed files with 39 additions and 0 deletions
0
django_components/templatetags/__init__.py
Normal file
0
django_components/templatetags/__init__.py
Normal file
12
django_components/templatetags/component_tags.py
Normal file
12
django_components/templatetags/component_tags.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from django import template
|
||||||
|
from django_components.component import registry
|
||||||
|
|
||||||
|
register = template.Library()
|
||||||
|
|
||||||
|
@register.simple_tag(name="component_dependencies")
|
||||||
|
def component_dependencies_tag():
|
||||||
|
out = []
|
||||||
|
for comp in registry._registry.values():
|
||||||
|
out.append(comp.render_dependencies())
|
||||||
|
|
||||||
|
return "\n".join(out)
|
||||||
27
tests/test_templatetags.py
Normal file
27
tests/test_templatetags.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
from textwrap import dedent
|
||||||
|
import unittest
|
||||||
|
from django.template import Template, Context
|
||||||
|
from django_components import component
|
||||||
|
from .django_test_setup import * # NOQA
|
||||||
|
|
||||||
|
class SimpleComponent(component.Component):
|
||||||
|
def context(self, variable=None):
|
||||||
|
return {
|
||||||
|
"variable": variable,
|
||||||
|
}
|
||||||
|
|
||||||
|
class Media:
|
||||||
|
template = "simple_template.html"
|
||||||
|
css = {"all": "style.css"}
|
||||||
|
js = ("script.js",)
|
||||||
|
|
||||||
|
class ComponentTemplateTagTest(unittest.TestCase):
|
||||||
|
def test_component_dependencies(self):
|
||||||
|
component.registry.register(name="testcomponent", component=SimpleComponent)
|
||||||
|
|
||||||
|
template = Template("{% load component_tags %}{% component_dependencies %}")
|
||||||
|
rendered = template.render(Context())
|
||||||
|
self.assertEqual(rendered, dedent("""
|
||||||
|
<link href="style.css" type="text/css" media="all" rel="stylesheet" />
|
||||||
|
<script type="text/javascript" src="script.js"></script>
|
||||||
|
""").strip())
|
||||||
Loading…
Add table
Add a link
Reference in a new issue