Login

Tag "tag"

Snippet List

If modulo template tag

Usage: {% ifmodulo forloop.counter 4 0 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %} or {% ifnotmodulo 5 3 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %} When the third parameter does not exist, it is assumed you're checking for values different than 0.

  • template
  • tag
  • if
  • modulo
Read More

Support for {% macro %} tags in templates, version 2

Tag library that provides support for *macros* in Django templates. **Usage example:** **0)** Save this file as <yourapp>/templatetags/macros.py **1)** In your template load the library: {% load macros %} **2)** Define a new macro called 'my_macro' with parameter 'arg1': {% macro my_macro arg1 %} Parameter: {{ arg1 }} {% endmacro %}` **3a)** Use the macro with a String parameter: {% usemacro my_macro "String parameter" %} **3b)** or with a variable parameter (provided the context defines 'somearg' variable, e.g. with value "Variable parameter"): {% usemacro my_macro somearg %} **3c)** **!!NEW!!** `usemacro` now also supports filters attached to parameters: {% usemacro my_macro "lowercase parameter"|upper %} Output of the above code would be: Parameter: String parameter Parameter: Variable parameter Parameter: LOWERCASE PARAMETER **4)** **!!NEW!!** Alternatively save your macros in a separate file, e.g. "mymacros.html" and load it into the current template with: {% loadmacros "mymacros.html" %} Macros can take *zero or more arguments* and both context variables and macro arguments are resolved in macro body when used in `{% usemacro ... %}` tag. Bear in mind that Macros are local to each template file and are not inherited through `{% extends ... %}` blocks.

  • template
  • tag
  • macro
  • usemacro
  • loadmacros
Read More

Menu/navigation bars in a tag

This tag makes it easy to include menu or navigation bars in an application's pages, even in a 'tabbed' fashion. (The actual appearance is styled in CSS; this example uses UL tags that are then supposed to be styled by a "display: inline" attribute to be rendered as horizontal bars.)

  • tag
  • menu
  • navigation
  • menubar
  • navbar
Read More
Author: ep
  • 4
  • 12

Random code tag

This tag solves for me a simple need: generate random and compact codes for some of my applications. Codes are not guaranteed to be unique. **Usage** {% randomCode "num" %} **Example**: {% randomCode "8" %} generates a random 8 characters code. Create a file on your favorite template tags folder called randomcode.py and paste the code. Don't forget to include {% load randomcode %} in your templates

  • tag
Read More

Inspect object debugging tag

Simple template tag that allows you to inspect an object in the context for debugging. It will inspect django models and forms using introspection. Dicts and lists are formatted to truncate long data. Pretty much everything else just displays the __str__ representation + class name. Example output: http://dsanity.net/introspectiontag.html Usage: put tag code as file introspection.py in your templatetags directory. Place the html template in your template path under "inspect_object.html". Then in your template: {% load introspection %} {% inspect_object obj "test object" %} The first parameter is the object to inspect, the second is the name used for display purposes.

  • tag
  • debug
  • debugging
  • introspection
Read More

If in list template tag

Given an item and a list, check if the item is in the list ----- item = 'a' list = [1, 'b', 'a', 4] ----- {% ifinlist item list %} Yup, it's in the list {% else %} Nope, it's not in the list {% endifinlist %}

  • template
  • tag
  • list
  • in
Read More

E-mail quoting filters and tags

Ttemplates and filters for quoting e-mails in templates. The `quoted_email` tag takes a template, renders it and quotes the result. The `quote_text` filter puts one or more levels of quotes around the passed text.

  • filter
  • tag
  • filters
  • email
  • tags
  • quoting
Read More

email_links

A very basic app centered around a template tag that takes an e-mail address and optional label and returns a link to an e-mail form. This way, you can easily throw e-mail addresses into your template that will automatically link to an e-mail form instead of displaying the e-mail address itself. The model for this app is extremely simple, with only one field to store the e-mail address. When an e-mail is passed to the email_link template tag, the e-mail address is added to the database if it is not already there. This is stored in the database in order to keep the full e-mail address hidden from the viewer. To use, simply create an app called 'email_links' (or you can change the name if you also change line 4 of email_extras.py to reflect the change). Then use the models.py and views.py provided. Add a templatetags directory under your app, and add email_extras.py and a blank __init__.py In your urls.py, add the following to your urlpatterns (subsitute project_name for your own): `(r'^email/(?P<email_id>[0-9]+)/$', 'project_name.email_links.views.email_form'),` Create two templates: email_links/email_form.html and email_links/thanks.html (the examples here are extremely basic - make them however you want). Finally, in a template where you want to provide a link to e-mail someone, first load email_extras: `{% load email_extras %}` Then do one of the following: `{% email_link "[email protected]" %}` `{% email_link "[email protected]" "E-mail someone" %}` Both will create a link to the e-mail form. The first will use mask_email (from the snippet by jkocherhans) to create the link. The second will display the second argument "E-mail someone" in the link. Also note that I've used the Sites add-on to generate the links, but you can easily change this to suit your needs. I hope all of this makes sense. I'm a bit tired, but wanted to get this up before bed.

  • filter
  • tag
  • email
Read More

template + cache = crazy delicious

A couple of utility `Node` subclasses that will automatically cache thier contents. Use `CachedNode` for template tags that output content into the template: class SomeNode(CachedNode): def get_cache_key(self, context): return "some-cache-key" def get_content(self, context): return expensive_operation() Use `CachedContextUpdatingNode` for tags that update the context: class AnotherNode(CachedContextUpdatingNode): # Only cache for 60 seconds cache_timeout = 60 def get_cache_key(self, context); return "some-other-cache-key" def get_content(self, context): return {"key" : expensive_operation()}

  • tag
  • templatetag
  • cache
Read More

Set Template Tag

Add a value to the context for easy access and for access from include templates. NOTE: This tag is composed from Django ticket: http://code.djangoproject.com/ticket/1322

  • tag
Read More
Author: Xin
  • 2
  • 3

Slightly better media path tag

A slightly improved version of [snippet #195](/snippets/195/) which keeps the logic but makes use of the `simple_tag` decorator to drastically simplify the code. For an alternative to this sort of tag, check out the media context processor in my [template_utils app](http://code.google.com/p/django-template-utils/).

  • template
  • tag
  • media
  • simple_tag
Read More

Handy tag for generating URLs to media files

Returns an absolute URL pointing to the given media file. The first argument is the path to the file starting from MEDIA_ROOT. If the file doesn't exist, empty string '' is returned. For example if you have the following in your settings: MEDIA_URL = 'http://media.example.com' then in your template you can get the URL for css/mystyle.css like this: {% media 'css/mystyle.css' %} This URL will be returned: http://media.example.com/css/style.css.

  • template
  • tag
  • media
Read More

"If in" templatetag

It's been a while, but I think I made this by copying and pasting the default {% if %} tag and modifying it. Does what it says, lets you test for inclusion in a list, in templates, like so: {% ifin item list %}.

  • template
  • lists
  • tag
  • contains
Read More

119 snippets posted so far.