In principle it's a DRY code of a template node that can be used for creating project-wide template-tags. These can be used to display conceptually common blocks in templates with applications specific looks and content. E.g.:
1. application-specific info message,
2. model instances details in the list context,
3. login messages like 'Please login or signup to [some app-dependent text]' with appropriate url's,
4. standard forms (e.g with same CSS classes, displayed with uni_form).
The code is profusely commented so look it up for details. Basically, when you have your project-wide `SubIncludeNode` tag defined, then just define appropriately named templates in applications template subdirectories e.g. `'profiles/_show_item.html'`.
** Example usage: login or signup message **
Let's say we are editing template rendered by the view indicated by the pattern named 'project_list' in the 'projects' app:
{# those are equivalent #}
{% login_or_signup %}
{% login_or_signup project_list %}
{% login_or_signup "project_list" %}
{# academic purposes direct sub_include usage #}
{# those two includes are also equivalent if there is no template called '_login_or_signup.html' in the 'projects' subdirectory #}
{% url acct_signup as signup_url %}
{% url acct_login as login_url %}
{% url project_list as next_url %}
{% sub_include "_login_or_signup.html" %}
{% include "_login_or_signup.html" %}
{# and those two are also equivalent if there is a template called '_login_or_signup.html' in the 'projects' subdirectory #}
{% sub_include "_login_or_signup.html" from "projects" %}
{% sub_include "_login_or_signup.html" %}
{# also equivalent include if there is no variable called 'projects' in tempalte's context #}
{% sub_include "_login_or_signup.html" from projects %}
{# those are examples of using custom subcontext #}
{% url acct_signup as signup_url %}
{% url acct_login as login_url %}
{% sub_include "_login_or_signup.html" from "default" with login_url,signup_url,next_url="/welcome" %}
{% sub_include "_login_or_signup.html" with login_url="/login",next_url="/welcome",signup_url="/signup" %}
** Mored advanced usage proposal **
Say you have an subclasses of some model from the original app in a separate extending apps. You want to display subclasses instances differently using the original app templates. In the original app you do not know what the set of subclasses is (as the original app is not aware of the extending apps). Subclassing the `SubIncludeNode` you can override the `default_subdir` method such that it returns the app label of a model instance that was passed as a argument of your tag. This way you just put templates like `_show_instance.html` in extending apps subdirectories and voila - you have implementation of the overrideable templates displaying appropriately details about the model subclass instances.
Accepts the same arguments as the 'range' builtin and creates
a list containing the result of 'range'.
Syntax:
{% mkrange [start,] stop[, step] as context_name %}
For example:
{% mkrange 5 10 2 as some_range %}
{% for i in some_range %}
{{ i }}: Something I want to repeat\n
{% endfor %}
Produces:
5: Something I want to repeat
7: Something I want to repeat
9: Something I want to repeat
A simple template tag that returns the favicon URL for a given arbitrary URL.
Put the code into a python module in the templatetags package of a Django app (e.g. myapp/templatetags/mytags.py), and use it like this:
{% load mytags %}
...
<img src="{% favicon posting.url %}/>
<a href="{{ posting.url }}">{{posting.title}}</a>
...
An update to snippet 1718 (http://www.djangosnippets.org/snippets/1718/). This update lets you pass an image URL string as well as a model ImageField instance. This means that you can then create thumbnails on demand for images outside of model
Django's standard inclusion_tag doesn't include context variables by default.
When you add takes_context you are required to manually merge the context
variables into the dict which your tag returns, which tends to result in
wasteful code or [possibly accidentally] leaking variables into the global
context (`context.update({…})`).
This decorator allows your inclusion tag to remain simple and still have safe
access to the global context for things like `MEDIA_URL`:
@register.inclusion_tag('my_template')
@private_context
def my_tag(context, …):
return {"foo": 1, "bar": 2}
A template tag which returns the n last tweets of a given user.
It uses the twitter python lib.
{% get_twitter_messages user foo limit 5 as tweets %}
{% for tweet in tweets %}
{{ tweet.text }} {{ tweet.time }} {{ tweet.url }}
{% endfor %}
If you're using [Django granular permissions](http://code.google.com/p/django-granular-permissions/), this templatetag may be useful.
It enables you to check permission in templates, as mentioned in the code:
{% has_row_perm user object "staff" as some_var %}
{% if some_var %}
...
{% endif %}
To be used in `if` statements, it always saves the result to the indicated context variable.
Put the snippet in row_perms.py in yourapp.templatetags package, and use `{% load row_perms %}` at your template.
A {% mailto %}{% endmailto %} template tag that requires an e-mail destination and optionally accepts subject, cc and bcc. It will then wrap whatever is within the tag in an `<a href="mailto:..."> </a>` HTML tag.
See the docstring in the code for the {% mailto %} usage and some examples.
You will need to load this template tag to your template. You can find detailed instructions [here](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout). But in a nutshell:
1. Create a templatetags package (meaning a directory with a __init__.py file in it) on the same level as your application's model.py
2. Put the code for this tag in a module (example: extra_tags.py)
3. On your template use {% load extra_tags %} -- note: the app where the templatetags package was created needs to be in INSTALLED_APPS
4. Use {% mailto user.email 'You subject here for {{user.get_full_name}}' %}blah{% endmailto %}
This is my first django template tag. I am also NOT tremendously experienced with Python. Criticism and corrections are more than welcome.
This tag builds on top of the [ifusergroup/else tag](http://www.djangosnippets.org/snippets/390/), fixes a small bug and introduces support for else blocks. This adds a way to provide multiple groups via group1|group2|group3
Inserts the output of a view, using fully qualified view name (and then some
args), a or local Django URL.
{% view view_or_url arg[ arg2] k=v [k2=v2...] %}
This might be helpful if you are trying to do 'on-server' AJAX of page
panels. Most browsers can call back to the server to get panels of content
asynchonously, whilst others (such as mobiles that don't support AJAX very
well) can have a template that embeds the output of the URL synchronously
into the main page. Yay! Go the mobile web!
Follow standard templatetag instructions for installing.
**IMPORTANT**: the calling template must receive a context variable called
'request' containing the original HttpRequest. This means you should be OK
with permissions and other session state.
**ALSO NOTE**: that middleware is not invoked on this 'inner' view.
Example usage...
Using a view name (or something that evaluates to a view name):
{% view "mymodule.views.inner" "value" %}
{% view "mymodule.views.inner" keyword="value" %}
{% view "mymodule.views.inner" arg_expr %}
{% view "mymodule.views.inner" keyword=arg_expr %}
{% view view_expr "value" %}
{% view view_expr keyword="value" %}
{% view view_expr arg_expr %}
{% view view_expr keyword=arg_expr %}
Using a URL (or something that evaluates to a URL):
{% view "/inner" %}
{% view url_expr %}
(Note that every argument will be evaluated against context except for the
names of any keyword arguments. If you're warped enough to need evaluated
keyword names, then you're probably smart enough to add this yourself!)
This is a conditional templatetag decorator that makes it *very* easy to write template tags that can be used as conditions. This can help avoid template boilerplate code (e.g. setting a variable in your template to be used in a condition).
All you have to do is define a function with expected parameters that returns True or False. Examples are in the code.