I often find myself needing to create a template tag and all I'm interested in is the token and the render function. This decorator abstracts away the boilerplate code around creating the template node class which is the same each and every time.
This get_sorted_items tag takes app.model, a number n, a a field name to sort ,and a variable-name as arguments and will deliver the n first objects of the model.
it checks if a Manager *publicmgr* exists and calls this if the user isn't authenticated.
Additionally it write the count of the model to the context
This template tag provides an easy way to render objects in your template, even if you don't know ahead of time what type they are.
For example, if you've got a search page with a result list comprised of objects from various models, you can simply loop through them and render them using the tag. The tag will choose the best template and render the object for you.
The tag's docstring has all the details. I hope you find this as useful as I have. Questions, comments, complaints welcome.
This Template Tag computes the font-size of two given arguments and returns a CSS-encoded string like "font-size: XXpx;", which can be used to format the font-size of link. (The minium font-size must be set with CSS.)
Requires two arguments:
1. occurrence of the current tag
2. sum of all occurrences
It works great for my tag-cloud.
[Source of the logarithmic formula](http://www.php.de/php-fortgeschrittene/44928-tag-cloud-algorithmus-fuer-schriftgroessye.html)
**Usage**
`<a href="http://www.anything.com" {% cloudify variable.occurrence overall. occurrence_sum %} title="anything">any tag</a>`
Template tag to obfuscate emails and other spam sensitive information.
Usage
obfuscate '[email protected]' 'Link display' 'Link title'
obfuscate '[email protected]' 'Link display'
obfuscate 'phone number'
Renders complex xhmtl compliant javascript.
May need caching as it renders a new code every time.
Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc.
This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.
Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr"
Example usage:
{% for entry in entries|sortby:'category.title' %}
Based on [1609](http://www.djangosnippets.org/snippets/1609/)
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!)
Adds a templatetag that works like an if block, but . The one and only argument is a float that reflects the percentage chance. It defaults to .2, %20.
{% sometimes %}
<img src='spy_behind_sniper.jpg'/>
{% else %}
<img src='sniper.jpg'/>
{% endsometimes %}
-- or --
{% sometimes .001 %}
You win!
{% else %}
Sorry, not a winner. Play again!
{% endsometimes %}
-- or --
{% sometimes .5 %}
This shows up half the time.
{% endsometimes %}
This is another partial tag, taken from a previous partial tag.
The previous one assumed template locations by hardcoding in "partials/%s", etc. I took all that out, and made it work. And I took out the not needed third parameter for passing in your own data
So now you call like this:
{% partial "partials/mypartial.html" %}
It passes the template context var into the partial, so anything you do in the main template, will work in the partial
Just make sure you've got all the right imports.
Is an updated way of splitting contents for a token, it does the split, but fixes the list..
EX:
From a tag call like this: {% partial "partials/template.html" %}
usually you get: ['partial','"partials/template.html"']
notice the " double quotes
fixes it with: ['partial','partials/template.html']
takes out the " quotes