mirror of
https://github.com/django-components/django-components.git
synced 2025-09-15 18:34:59 +00:00
Added template loader for component templates automatic discovery (thanks @Real-Gecko)
This commit is contained in:
parent
74d7ea008b
commit
c1d090d5c0
2 changed files with 40 additions and 6 deletions
35
README.md
35
README.md
|
@ -26,7 +26,7 @@ Install the app into your environment:
|
||||||
|
|
||||||
Then add the app into INSTALLED APPS in settings.py
|
Then add the app into INSTALLED APPS in settings.py
|
||||||
|
|
||||||
```
|
```python
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
...,
|
...,
|
||||||
"django_components",
|
"django_components",
|
||||||
|
@ -34,11 +34,33 @@ INSTALLED_APPS = [
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Modify `TEMPLATES` section of settings.py as follows:
|
||||||
|
- Remove `'APP_DIRS': True,`
|
||||||
|
- add `loaders` to `OPTIONS` list and set it to following value:
|
||||||
|
```python
|
||||||
|
TEMPLATES = [
|
||||||
|
{
|
||||||
|
...,
|
||||||
|
'OPTIONS': {
|
||||||
|
'context_processors': [
|
||||||
|
...
|
||||||
|
],
|
||||||
|
'loaders':[(
|
||||||
|
'django.template.loaders.cached.Loader', [
|
||||||
|
'django.template.loaders.filesystem.Loader',
|
||||||
|
'django.template.loaders.app_directories.Loader',
|
||||||
|
'django_components.template_loader.Loader',
|
||||||
|
]
|
||||||
|
)],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
```
|
||||||
## Optional
|
## Optional
|
||||||
|
|
||||||
To avoid loading the app in each template using ``` {% load django_components %} ```, you can add the tag as a 'builtin' in settings.py
|
To avoid loading the app in each template using ``` {% load django_components %} ```, you can add the tag as a 'builtin' in settings.py
|
||||||
|
|
||||||
```
|
```python
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
...,
|
...,
|
||||||
|
@ -122,7 +144,7 @@ A component in django-components is the combination of four things: CSS, Javascr
|
||||||
First you need a CSS file. Be sure to prefix all rules with a unique class so they don't clash with other rules.
|
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
|
```css
|
||||||
/* In a file called style.css */
|
/* In a file called [your app]/components/calendar/style.css */
|
||||||
.calendar-component { width: 200px; background: pink; }
|
.calendar-component { width: 200px; background: pink; }
|
||||||
.calendar-component span { font-weight: bold; }
|
.calendar-component span { font-weight: bold; }
|
||||||
```
|
```
|
||||||
|
@ -130,7 +152,7 @@ First you need a CSS file. Be sure to prefix all rules with a unique class so th
|
||||||
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.
|
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
|
```js
|
||||||
/* In a file called script.js */
|
/* In a file called [your app]/components/calendar/script.js */
|
||||||
(function(){
|
(function(){
|
||||||
$(".calendar-component").click(function(){ alert("Clicked calendar!"); })
|
$(".calendar-component").click(function(){ alert("Clicked calendar!"); })
|
||||||
})()
|
})()
|
||||||
|
@ -139,7 +161,7 @@ Then you need a javascript file that specifies how you interact with this compon
|
||||||
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.
|
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
|
```htmldjango
|
||||||
{# In a file called calendar.html #}
|
{# In a file called [your app]/components/calendar/calendar.html #}
|
||||||
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -157,8 +179,9 @@ class Calendar(component.Component):
|
||||||
"date": date,
|
"date": date,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Note that Django will look for templates inside `[your app]/components` dir
|
||||||
def template(self, context):
|
def template(self, context):
|
||||||
return "[your app]/components/calendar/calendar.html"
|
return "calendar/calendar.html"
|
||||||
|
|
||||||
class Media:
|
class Media:
|
||||||
css = '[your app]/components/calendar/calendar.css'
|
css = '[your app]/components/calendar/calendar.css'
|
||||||
|
|
11
django_components/template_loader.py
Normal file
11
django_components/template_loader.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
"""
|
||||||
|
Template loader that loads templates from each Django app's "components" directory.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.template.loaders.filesystem import Loader as FilesystemLoader
|
||||||
|
from django.template.utils import get_app_template_dirs
|
||||||
|
|
||||||
|
|
||||||
|
class Loader(FilesystemLoader):
|
||||||
|
def get_dirs(self):
|
||||||
|
return get_app_template_dirs('components')
|
Loading…
Add table
Add a link
Reference in a new issue