mirror of
https://github.com/django-components/django-components.git
synced 2025-08-31 11:17:21 +00:00
Improve README by adding detailed instructions.
This commit is contained in:
parent
7bc1096e94
commit
62c556f530
1 changed files with 52 additions and 5 deletions
57
README.md
57
README.md
|
@ -5,9 +5,35 @@ A way to create simple reusable template components in Django.
|
|||
# Installation
|
||||
```pip install django-components``` (NOTE: Does not work yet)
|
||||
|
||||
# Usage
|
||||
# Create your first component
|
||||
|
||||
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.
|
||||
A component in django-components is the combination of four things: CSS, Javascript, a Django template, and some Python code to put them all together.
|
||||
|
||||
First you need a CSS file. Be sure to prefix all rules with a unique class so they don't clash with other rules.
|
||||
|
||||
```css
|
||||
/* In a file called style.css */
|
||||
.calendar-component { width: 200px; background: pink; }
|
||||
.calendar-component span { font-weight: bold; }
|
||||
```
|
||||
|
||||
Then you need a javascript file that specifies how you interact with this component. You are free to use any javascript framework you want. A good way to make sure this component doesn't clash with other components is to define all code inside an anonymous function that calls itself. This makes all variables defined only be defined inside this component and not affect other components.
|
||||
|
||||
```js
|
||||
/* In a file called script.js */
|
||||
(function(){
|
||||
$(".calendar-component").click(function(){ alert("Clicked calendar!"); })
|
||||
})()
|
||||
```
|
||||
|
||||
Now you need a Django template for your component. Feel free to define more variables like `date` in this example. When creating an instance of this component we will send in the values for these variables. The template will be rendered with whatever template backend you've specified in your Django settings file.
|
||||
|
||||
```htmldjango
|
||||
{# In a file called template.html #}
|
||||
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
||||
```
|
||||
|
||||
Finally, we use django-components to tie this together. We create a Component by inheriting from the Component class and specifying the context method. We also register the global component registry so that we easily can render it anywhere in our templates.
|
||||
|
||||
```python
|
||||
from django_components import component
|
||||
|
@ -26,7 +52,11 @@ class Calendar(component.Component):
|
|||
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.
|
||||
And voilá! We've created our first component.
|
||||
|
||||
# Use the component in a template
|
||||
|
||||
First load the `django_components` tag library, then use the `component_dependencies` and `component` tags to render the component to the page.
|
||||
|
||||
```htmldjango
|
||||
{% load django_components %}
|
||||
|
@ -37,12 +67,29 @@ In your templates, use your component by first importing the django_components t
|
|||
{% component_dependencies %}
|
||||
</head>
|
||||
<body>
|
||||
{% component name="calendar" date=custom_date1 %}
|
||||
{% component name="calendar" date=custom_date2 %}
|
||||
{% component name="calendar" date="2015-06-19" %}
|
||||
</body>
|
||||
<html>
|
||||
```
|
||||
|
||||
The output from the above template will be:
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My example calendar</title>
|
||||
<link href="style.css" type="text/css" media="all" rel="stylesheet" />
|
||||
<script type="text/javascript" src="script.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="calendar-component">Today's date is <span>2015-06-19</span></div>
|
||||
</body>
|
||||
<html>
|
||||
```
|
||||
|
||||
This makes it possible to organize your front-end around reusable components. Instead of relying on template tags and keeping your CSS and Javascript in the static directory.
|
||||
|
||||
# Running the tests
|
||||
|
||||
Install `tox`:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue