mirror of
https://github.com/django-components/django-components.git
synced 2025-08-04 14:28:18 +00:00
docs: fix links in README and "overview" section, add tutorial (#842)
This commit is contained in:
parent
6813c9d7aa
commit
789f3807aa
27 changed files with 1213 additions and 216 deletions
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Accessing component inputs
|
||||
weight: 5
|
||||
weight: 3
|
||||
---
|
||||
|
||||
When you call `Component.render` or `Component.render_to_response`, the inputs to these methods can be accessed from within the instance under `self.input`.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Autodiscovery
|
||||
weight: 11
|
||||
weight: 9
|
||||
---
|
||||
|
||||
Every component that you want to use in the template with the [`{% component %}`](django_components.templateags.component_tags)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Component context and scope
|
||||
weight: 6
|
||||
weight: 4
|
||||
---
|
||||
|
||||
By default, context variables are passed down the template as in regular Django - deeper scopes can access the variables from the outer scopes. So if you have several nested forloops, then inside the deep-most loop you can access variables defined by all previous loops.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Components as views
|
||||
weight: 12
|
||||
weight: 10
|
||||
---
|
||||
|
||||
_New in version 0.34_
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Components in Python
|
||||
weight: 4
|
||||
weight: 2
|
||||
---
|
||||
|
||||
_New in version 0.81_
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
---
|
||||
title: Components in templates
|
||||
weight: 3
|
||||
---
|
||||
|
||||
First load the `component_tags` tag library, then use the `component_[js/css]_dependencies` and `component` tags to render the component to the page.
|
||||
|
||||
```htmldjango
|
||||
{% load component_tags %}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My example calendar</title>
|
||||
{% component_css_dependencies %}
|
||||
</head>
|
||||
<body>
|
||||
{% component "calendar" date="2015-06-19" %}{% endcomponent %}
|
||||
{% component_js_dependencies %}
|
||||
</body>
|
||||
<html>
|
||||
```
|
||||
|
||||
> NOTE: Instead of writing `{% endcomponent %}` at the end, you can use a self-closing tag:
|
||||
>
|
||||
> `{% component "calendar" date="2015-06-19" / %}`
|
||||
|
||||
The output from the above template will be:
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My example calendar</title>
|
||||
<link
|
||||
href="/static/calendar/style.css"
|
||||
type="text/css"
|
||||
media="all"
|
||||
rel="stylesheet"
|
||||
/>
|
||||
</head>
|
||||
<body>
|
||||
<div class="calendar-component">
|
||||
Today's date is <span>2015-06-19</span>
|
||||
</div>
|
||||
<script src="/static/calendar/script.js"></script>
|
||||
</body>
|
||||
<html></html>
|
||||
</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.
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Defining HTML / JS / CSS files
|
||||
weight: 10
|
||||
weight: 8
|
||||
---
|
||||
|
||||
django_component's management of files builds on top of [Django's `Media` class](https://docs.djangoproject.com/en/5.0/topics/forms/media/).
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: HTML attributes
|
||||
weight: 9
|
||||
weight: 7
|
||||
---
|
||||
|
||||
_New in version 0.74_:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Single-file components
|
||||
weight: 2
|
||||
weight: 1
|
||||
---
|
||||
|
||||
Components can also be defined in a single file, which is useful for small components. To do this, you can use the `template`, `js`, and `css` class attributes instead of the `template_name` and `Media`. For example, here's the calendar component from above, defined in a single file:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Slots
|
||||
weight: 8
|
||||
weight: 6
|
||||
---
|
||||
|
||||
_New in version 0.26_:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
title: Template tag syntax
|
||||
weight: 7
|
||||
weight: 5
|
||||
---
|
||||
|
||||
All template tags in django_component, like `{% component %}` or `{% slot %}`, and so on,
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
---
|
||||
title: Create your first component
|
||||
weight: 1
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```
|
||||
sampleproject/
|
||||
├── calendarapp/
|
||||
├── components/ 🆕
|
||||
│ └── calendar/ 🆕
|
||||
│ ├── calendar.py 🆕
|
||||
│ ├── script.js 🆕
|
||||
│ ├── style.css 🆕
|
||||
│ └── template.html 🆕
|
||||
├── sampleproject/
|
||||
├── manage.py
|
||||
└── requirements.txt
|
||||
```
|
||||
|
||||
Start by creating empty files in the structure above.
|
||||
|
||||
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 title="[project root]/components/calendar/style.css"
|
||||
/* In a file called [project root]/components/calendar/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 title="[project root]/components/calendar/script.js"
|
||||
/* In a file called [project root]/components/calendar/script.js */
|
||||
(function () {
|
||||
if (document.querySelector(".calendar-component")) {
|
||||
document.querySelector(".calendar-component").onclick = 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 title="[project root]/components/calendar/calendar.html"
|
||||
{# In a file called [project root]/components/calendar/template.html #}
|
||||
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
||||
```
|
||||
|
||||
Finally, we use django-components to tie this together. Start by creating a file called `calendar.py` in your component calendar directory. It will be auto-detected and loaded by the app.
|
||||
|
||||
Inside this file 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 title="[project root]/components/calendar/calendar.py"
|
||||
# In a file called [project root]/components/calendar/calendar.py
|
||||
from django_components import Component, register
|
||||
|
||||
@register("calendar")
|
||||
class Calendar(Component):
|
||||
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
|
||||
# will be automatically found.
|
||||
#
|
||||
# `template_name` can be relative to dir where `calendar.py` is, or relative to COMPONENTS.dirs
|
||||
template_name = "template.html"
|
||||
# Or
|
||||
def get_template_name(context):
|
||||
return f"template-{context['name']}.html"
|
||||
|
||||
# This component takes one parameter, a date string to show in the template
|
||||
def get_context_data(self, date):
|
||||
return {
|
||||
"date": date,
|
||||
}
|
||||
|
||||
# Both `css` and `js` can be relative to dir where `calendar.py` is, or relative to COMPONENTS.dirs
|
||||
class Media:
|
||||
css = "style.css"
|
||||
js = "script.js"
|
||||
```
|
||||
|
||||
And voilá!! We've created our first component.
|
Loading…
Add table
Add a link
Reference in a new issue