Login

Tag "template"

Snippet List

Seconds-to-Duration Template Tag

Does exactly what it says on the tin! This template tag, when implemented, converts a duration (in seconds) to a more meaningful format. It has a short and long setting, which is easy to manipulate for your needs. Apologies if something already exists like this, however I felt that writing this would be quicker than trying to find it online. As an example, given the duration 84658: Short (default): 23 hrs 30 mins 58 secs Long: 23 hours, 30 minutes and 58 seconds All the best, [Dan Ward](http://d-w.me).

  • template
  • tag
  • seconds
  • to
  • duration
Read More

Load template from specific app

You can use this template loader if you want to use template specifically from one app. Useful mainly in overriding admin templates - just make your own `admin/change_form.html` and have it extend `admin:admin/change_form.html` withou creating any symlinking or copying django admin's templates to alternate location. Part of the [ella project](http://www.ellaproject.cz/).

  • template
  • loader
  • app
Read More

render_as_template template tag

This is a template tag that works like `{% include %}`, but instead of loading a template from a file, it uses some text from the current context, and renders that as though it were itself a template. This means, amongst other things, that you can use template tags and filters in database fields. For example, instead of: `{{ flatpage.content }}` you could use: `{% render_as_template flatpage.content %}` Then you can use template tags (such as `{% url showprofile user.id %}`) in flat pages, stored in the database. The template is rendered with the current context. Warning - only allow trusted users to edit content that gets rendered with this tag.

  • template
  • tag
  • context
Read More

Variable resolving URL template tag

** DEPRECATED**, use [django-reversetag @ github](http://github.com/ulope/django-reversetag/tree/master) instead. If you want to be able to use context variables as argument for the "url" template tag this is for you. Just put this code somwhere where it will be run early (like your app's _ _init_ _.py) and of you go. Usage: {% url name_of_view_or_variable arg1 arg2 %} **NOTE:** This may possibly break your site! Every view name that is passed to url will be tried to be resolved as a context variable first! So if there is a variable coincidentally named like one of your views THEN IT WILL BREAK. So far it works great for me, but keep an eye out for name clashes.

  • template
  • tag
  • url
  • context
  • variable
  • resolve
  • resolving
Read More

Template range filter

Easy to use range filter. Just in case you have to use a "clean" for loop in the template. Inspired by [Template range tag](http://www.djangosnippets.org/snippets/779/) Copy the file to your templatetags and load them. [Django doc | Custom template tags and filters](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)

  • template
  • filter
  • range
Read More

Fuzzy Date Diff Template Filter

Pass in a date and you get a humanized fuzzy date diff; e.g. "2 weeks ago" or "in 5 months". The date you pass in can be in the past or future (or even the present, for that matter). The result is rounded, so a date 45 days ago will be "2 months ago", and a date 400 days from now will be "in 1 year". Usage: * `{{ my_date|date_diff }}` will give you a date_diff between `my_date` and `datetime.date.today()` * `{{ my_date|date_diff:another_date }}` will give you a date_diff between `my_date` and `another_date` Make sure to install this as a template tag and call `{% load date_diff %}` in your template; see the [custom template tag docs](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/) if you don't know how to do that.

  • template
  • filter
  • date
  • humanize
Read More

inclusion tag with template as variable

Sometimes you need to write a tag that renders other template, but the template name depends on template tag arguments. Usually you use simple_tag or write your own Node class. Here is a simple aproach that uses inclusion_tag. This way you can use context objects when used with takes_context=True

  • template
  • tag
Read More

More flexible "Partial Template"

This is an extension of the snippet http://www.djangosnippets.org/snippets/1302/ to make it a bit more flexible and been able pass more than one parameter to our "Partial Template". To use it you can {% partial_template template_name param1:variable1 param2:variable2 ... %} or: {% partial_template partials/mini_template.html item:data1 var2:"False" var3:"2*2" %}

  • template
  • templatetag
  • include
Read More

"Partial Templates" - an alternative to "include"

This snippet adds simple partial support to your templates. You can pass data to the partial, and use it as you would in a regular template. It is different from Django's `{% include %}`, because it allows you to pass a custom variable (context), instead of reusing the same context for the included template. This decouples the templates from each other and allows for their greater reuse. The attached code needs to go into `templatetags` folder underneath your project. The usage is pretty simple - `{% load ... %}` the tag library, and use `{% partial_template template-name data %}` in your template. This will result in template passed as **template-name** to be loaded from **partials** folder. The **.html** extension will be appended to the file name. The file has to be in one of template paths accessible to the loader) and rendered with **data** as its context. The data is available in the template as an `item` context variable. You can find more information in the [relevant Django documentation](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags)

  • template
  • tag
  • templates
  • tags
  • partial
  • include
  • partials
Read More

keywords arguments parser for custom template tags

returns a list of (argname, value) tuples (NB: keeps ordering and is easily turned into a dict). Params: * tagname : the name of calling tag (for error messages) * bits : sequence of tokens to parse as kw args * args_spec : (optional) dict of argname=>validator for kwargs, cf below * restrict : if True, only argnames in args_specs will be accepted If restrict=False and args_spec is None (default), this will just try to parse a sequence of key=val strings. About args_spec validators : * A validator can be either a callable, a regular expression or None. * If it's a callable, the callable must take the value as argument and return a (possibly different) value, which will become the final value for the argument. Any exception raised by the validator will be considered a rejection. * If it's a regexp, the value will be matched against it. A failure will be considered as a rejection. * Using None as validator only makes sense with the restrict flag set to True. This is useful when the only validation is on the argument name being expected.

  • template
  • tag
  • custom
  • template_tags
Read More

DaGood breadcrumbs

Provides two template tags to use in your HTML templates: breadcrumb and breadcrumb_url. The first allows creating of simple url, with the text portion and url portion. Or only unlinked text (as the last item in breadcrumb trail for example). The second, can actually take the named url with arguments! Additionally it takes a title as the first argument. This is a templatetag file that should go into your /templatetags directory. Just change the path of the image in the method **create_crumb** and you are good to go! Don't forget to `{% load breadcrumbs %}` at the top of your html template! [http://drozdyuk.blogspot.com/2009/02/dagood-django-breadcrumbs.html](http://drozdyuk.blogspot.com/2009/02/dagood-django-breadcrumbs.html)

  • template
  • tags
  • navigation
  • breadcrumbs
Read More

Truncate string after a given number of chars keeping whole words

Truncates a string after a given length, keeping the last word complete. This filter is more precise than the default `truncatewords` filter. Words length vary too much, 10 words may result in 40 or 70 characters, so cutting by character count makes more sense. There is a [blog post](http://ricobl.wordpress.com/2008/12/23/templates-django-filtro-truncatewords-melhorado/) about this snippet (in Portuguese).

  • template
  • filter
  • templatetag
  • truncate
  • templatetags
  • words
Read More
Author: rix
  • 5
  • 6

Safing HTML Text Input

Personally I hate using markdown for text input just so it can be converted into HTML. Markdown languages almost always don't support some thing I want to do; thus, why not just use HTML in the first place. Well because you don't want anybody posting any kind of HTML on your site. Solution, instead of making your users learn markdown, let them enter HTML and filter out bad tags. This is a filter I use to filter HTML for only certain allowed tags. The allowed tags can be configured with the **allowedhtml** list. To make your text input even more user friendly use a Javascript HTML editor like [FCK Editor](http://www.fckeditor.net/) so your users will have a nice GUI editor.

  • template
  • filter
  • markup
  • markdown
  • html
Read More

262 snippets posted so far.