mirror of
https://github.com/django-components/django-components.git
synced 2025-08-04 14:28:18 +00:00
Create simple reusable template components in Django.
https://django-components.github.io/django-components
![]() This started out as a plan to support Django 1.7 and 1.8, and on all current versions of Python. This turned into supporting Django 1.8 on Python 2.7, 3.3, and 3.4 only. The tests don't pass under Django 1.7 because 1.8 introduced new settings for configuring templates. Because of this the install_requires section of setup.py declares that it must be using Django 1.8+. Python 3.3 support was added, since that's a supported version of Python under Django 1.8. Also added pytest-xdist to the tox dependencies, to support running tox -e py34 -- -f in order to have it watch and rerun the tests in development. |
||
---|---|---|
django_components | ||
tests | ||
.gitignore | ||
.travis.yml | ||
README.md | ||
setup.py | ||
tox.ini |
django-components
A way to create simple reusable template components in Django.
Installation
pip install django-components
(NOTE: Does not work yet)
Usage
Start by creating a Component by inheriting from the Component class. Don't forget to register the component so that it's available in the templates.
from django_components import component
class Calendar(component.Component):
def context(self, date):
return {
"date": date,
}
class Media:
template = "[your app]/components/calendar/calendar.html"
css = {'all': ('[your app]/components/calendar/calendar.css',)}
js = ('[your app]/components/calendar/calendar.js',)
component.registry.register(name="calendar", component=Calendar)
In your templates, use your component by first importing the django_components tag library, and then using the component_dependencies and component tags to render the component to the page.
{% load django_components %}
<!DOCTYPE html>
<html>
<head>
<title>My example calendar</title>
{% component_dependencies %}
</head>
<body>
{% component name="calendar" date=custom_date1 %}
{% component name="calendar" date=custom_date2 %}
</body>
<html>
Running the tests
Install and run tox
:
pip install tox
tox