Every repository with this icon (
Every repository with this icon (
tree 25741ba247841fc5a60c84a096e3e1a56244683f
parent c09da4c1946771c3df04df23e6bfeae2820c607f
| name | age | message | |
|---|---|---|---|
| |
.gitignore | ||
| |
LICENSE.txt | ||
| |
README.rst | ||
| |
__init__.py | ||
| |
management | ||
| |
setup.py | ||
| |
templatecomponents/ | ||
| |
views.py |
django-templatecomponents
| Author: | Filip Noetzel |
|---|---|
| Version: | v0.03 |
| Web: | http://j03.de/projects/django-templatecomponents/ |
| Git: | git clone http://j03.de/git/django-templatecomponents.git/ ( browse, also on github) |
| Download: | django-templatecomponents.tar.gz |
A django application that makes it easy to organize your component source (JavaScript, CSS) in your django templates.
Overview
Define your JavaScript and CSS source right beneath the HTML skeleton that it's used on:
template.html:
This would result in
print.css:
screen.css:
client.js:
Benefits
Serve your components from one file
(see also rule #1)
You can arrange your template component blocks in (multiple) groups, to access them by different urls (Here, print.js will contain the concatenated content of the first two blocks).
One can imagine groups for
- printing CSS
- screen CSS
- presentation CSS
- additional CSS and JavaScript for authenticated / paying users
- CSS for browers with enabled or disabled JavaScript
- CSS and JavaScript for mobile devices
- CSS and JavaScript for legacy browsers
- Splitting the initial payload
- Splitting JavaScript in 25K-Slices for the iPhone
Usage
Static file generation
While you want your template components be processed on the fly while developing, you can generate static files from your template components upon each deployment:
Priority based block dependency
Some CSS Rules and JavaScript might depend on each other (Specific CSS rules override basic CSS Rules; some of your JavaScript depends on your favorite ajax library).
Each block can have a priority, the following example illustrates this:
template1.html:
template2.html:
This would ensure, the javascript block from template2.html appears above the one from template1.html:
xlib.js:
It is recommended to give a high priority for JavaScript libraries, a lower for custom built library code and a very low priority for custom code snippets.
Including external libraries
You can easily include additional static files (like JavaScript libraries, CSS frameworks, ..), by specifying them in your settings.py:
This way, you can avoid putting third party code in your templates/ directory and adding django template tags in the first and last line.
Preprocessing JavaScript and CSS with Django Templates
You can use every aspect of the django template language and all the builtin template tags and filters.
However, the context that is available within the templatecomponent-tags only contains settings (with the contents of your settings.py).
Note
The {% css %} and {% js %} blocks are evaluated once at deployment time, when you generate the static files.
Installation
Download
Using git:
Using tarball:
Put the folder django-templatecomponents somewhere in your $PYTHONPATH (presumably your project folder, where your manage.py lives).
Misc
Syntax highlighting in vim
To get the syntax highlighting for the (now embedded) css and javascript in vim, create a file at ~/.vim/after/syntax/htmldjango.vim with the following contents:
What next ?
- Convert all your components to template components.
- Read Steve Souder's "High Performance Web Sites"
- See django-hashedmedia to speed up your loading times even further.












