2 Name: django-insert-above
4 Summary: These django templatetags is a hack making possible to insert "content" in some (maybe above the current or parent template) places.
5 Home-page: https://github.com/yunmanger1/django-insert-above/
7 Author-email: germanilyin@gmail.com
9 Description: WHAT IS IT?
12 These templatetags is a hack making possible to insert 'content' in
13 some (maybe above the current or parent template) places.
15 More specifically, when we use these tags, there are some Nodes called
16 'containers' that are rendered after all other nodes are rendered, but placed
17 in it's right posistion. Using this hack, 'containers' may render
18 depending on variables in context that were generated by nodes placed anywhere in
19 template (maybe after it).
21 It's very useful in cases when you are reusing some template parts
22 very often. For example displaying comment list and comment submit form.
23 We write some template and put it into comments.html. Then every time
24 we need comments we just {% include "comments.html" %}.
25 But what if this part needs some js or css? Then we need to create
26 some comments-jscss.html and override some {% block head %}. IMHO this
27 is quite inconvenient.
29 Using this tool we can insert js and css into head
30 directly from comments.html
35 1. Create convenient way to include media resources in head of HTML page.
36 2. Handle repetition of resource includes.
37 3. Make it possible to require resources from included templates.
38 4. Keep the order of resource includes from different places.
43 1. (required) add 'insert_above' in INSTALLED_APPS in your settings.py
45 2. (optional) add these two lines of code somewhere in your project where
46 they will run for sure. For example in urls.py
49 from django.template.loader import add_to_builtins
50 add_to_builtins('insert_above.templatetags.insert_tags')
56 1. {% insert_handler %}
57 2. {% container name %}
58 3. {% media_container name %}
59 4. {% insert_str container str %}
60 5. {% insert container %}{% endinsert %}
61 6. media_tag filter simply converts `ga.js` into `<script type='text/javascript' src='/static/ga.js'></script>`
66 1. `{% container %}` or `{% media_container %}` tags must NOT be in other `{% block %}`.
67 2. `{% insert_handler %}` ought to be at ther very beginning of base template.
72 1. `IA_USE_MEDIA_PREFIX`, by default True
73 2. `IA_MEDIA_PREFIX`, if not set `STATIC_URL` is used, if not set `MEDIA_URL` is used, if not set '/media/' is used
74 3. `DEBUG`, if True logs how much time spent on rendering
75 4. `IA_JS_FORMAT`, by default `<script type='text/javascript' src='{URL}'></script>`
76 5. `IA_CSS_FORMAT`, by default `<link rel='stylesheet' href='{URL}' type='text/css' />`
77 6. `IA_MEDIA_EXTENSION_FORMAT_MAP`, by default `{'css' : CSS_FORMAT, '.js' : JS_FORMAT}`
82 Let's analyze an example.
91 <script type='text/javascript' src='{{ MEDIA_URL }}js/jquery.min.js'></script>
92 {% media_container media %}
94 $(document).ready(function(){
106 Base template creating blocks and containers..
111 {% extends "base.html" %}
114 {% insert_str media "js/mathjax.js" %}
115 {% block header %}{% endblock %}
116 {% block menu %}{% include "blog/menu.html" %}{% endblock %}
117 {% block text %}{% endblock %}
118 {% block footer %}{% endblock %}
122 Extending content block. Requiring js/mathjax.js resource into 'media' container.
127 {% insert_str media "js/animated.menu.js" %}
128 {% insert_str media "css/animated.menu.css" %}
130 $('ul.menu').each(function(){
131 $(this).superanimation();
134 <ul id='blog-menu' class='menu'>
141 Requiring js/animated.menu.js and css/animated.menu.css into "media" container.
142 Inserting javascript code into "ready" container.
144 blog/post_detail.html
147 {% extends "blog/base.html" %}
149 {% block header %}{{ title }}{% endblock %}
152 {% insert_str media "js/mathjax.js" %}
161 Implementing blocks and requiring js/mathjax.js into media.
165 Template('blog/post_detail.html').render(Context({'title': 'Hello', 'text': 'World'}))
172 <script type='text/javascript' src='/media/js/jquery.min.js'></script>
173 <script type='text/javascript' src='/media/js/mathjax.js'></script>
174 <script type='text/javascript' src='/media/js/animated.menu.js'></script>
175 <link rel="stylesheet" href="/media/css/animated.menu.css" type="text/css" />
177 $(document).ready(function(){
178 $('ul.menu').each(function(){
179 $(this).superanimation();
186 <ul id='blog-menu' class='menu'>
200 1. `js/mathjax.js` automatically becomes `<script type='text/javascript' src='/media/js/mathjax.js'></script>`
201 and `css/animated.menu.css` becomes `<link rel="stylesheet" href="/media/css/animated.menu.css" type="text/css" />`
202 2. inserting from included template is possible
203 3. any text may be inserted to any container. Here we insert javascript code in `$(document).ready(function(){});`
204 4. `js/mathjax.js` was required twice, but included only once.
205 5. The order of included resources is kept.
212 + **fix MEDIA_URL setting**
213 if STATIC_URL is not set in settings, it's value is None by default in new versions of Django.
214 Now we check if STATIC_URL is None, then use MEDIA_URL
218 + added new tag `{% insert_form container form %}`
219 + added new tag `{% insert_form container form.media %}`
229 Classifier: Development Status :: 3 - Alpha
230 Classifier: Topic :: Utilities
231 Classifier: Environment :: Plugins
232 Classifier: Framework :: Django
233 Classifier: Intended Audience :: Developers
234 Classifier: License :: Freeware
235 Classifier: Programming Language :: Python :: 2.6