Lets say we want a library of javascript-based pieces of functionality (I’m thinking jquery):
For example:
- an ajax dialog
- a date picker
- a form validator
- a sliding menu bar
- an accordian thingy
There are four pieces of code for each: some Python, CSS, JS, & HTML.
What is the best way to arrange all these pieces so that:
- each javascript ‘module’ can be neatly reused by different views
- the four bits of code that make up the completed function stay together
- the css/js/html parts appear in their correct places in the response
- common dependencies between modules are not repeated (eg: a javascript file in common)
x————–
It would be nice if, or is there some way to ensure that, when called from a templatetag, the templates respected the {% block %} directives. Thus one could create a single template with a block each for CSS, HTML, and JS, in a single file. Invoke that via a templatetag which is called from the template of whichever view wants it. That make any sense. Can that be done some way already? My templatetag templates seem to ignore the {% block %} directives.
x————–
There’s some very relevant gasbagging about putting such media in forms here http://docs.djangoproject.com/en/dev/topics/forms/media/ which probably apply to the form validator and date picker examples.
Been a while since I posted this problem. What I’ve been doing to solve it is:
Restraint is required to write it in such a way that it acts as a client side script only; don’t be tempted to try and inject values from the server at the time of serving the js. Ultimately I’ve found it less confusing to apply server side variables strictly in the html template.
In this way I’m able to:
It’s not perfect, but it’s getting me by till a neater idea comes along.
For example a js library in “media/js/alertlib.js” might include:
and the template has: