Login

Tag "template"

Snippet List

[UPDATE]Filter to resize a ImageField on demand

A filter to resize a ImageField on demand, a use case could be: <img src="{{ object.image.url }}" alt="original image"> <img src="{{ object.image|thumbnail }}" alt="image resized to default 104x104 format"> <img src="{{ object.image|thumbnail:200x300 }}" alt="image resized to 200x300"> Original http://www.djangosnippets.org/snippets/192/

  • template
  • image
  • thumbnail
Read More

Minify JS template tag

Uses JSMin. Python version available from [http://www.crockford.com/javascript/jsmin.py.txt](http://www.crockford.com/javascript/jsmin.py.txt) Provides template tags to minify JavaScript on the fly. `{% minifyjs %}[code]{% endminifyjs %}`

  • template
  • tag
  • javascript
  • js
  • minify
Read More

Refactor template files to use {% block %} tags.

Refactors two similar django template files to use `{% block %}` tags. Takes two template files, where one is a modified version of the other, and looks for differences and similiarities between the two. It then generates refactored (and renamed) versions of each file and a third 'base' file, adding an `{% extends %}` tag as appropriate. Block tags are named with matching numbers in all 3 files, making it easy to do a search and replace with more meaningful labels. `sample_data_in_base` controls whether or not the content from file 1 is copied into place in the base file as an example. Problems: it doesn't identify open `{% for %}` or `{% if %}` tags, so this needs some manual fixing (moving the `{% endfor %}` and `{% endif %}` tags into the proper blocks). It doesn't add the correct path for the `{% extends %}` tag either (ie. `"app/base.html"`). `collapse_whitespace` is not working at all.

  • template
  • refactor
Read More

Render specific blocks from templates (useful for AJAX, alternative)

Special thanks to the author of snippet 769 who provided most of the code for this snippet. Major differences: 1.Simpler/better handling of "extends" block tag 2.Searches If/Else blocks 3.Less code 4.Allow list of templates to be passed which is closer to the behavior of render_to_response

  • template
  • block
  • templates
  • render
  • context
  • blocks
Read More

Replace Paragraph Tags for Flash

This template tag does two things needed to display content from something like a TextField wrapped with TinyMCE in Flash's htmlText component: 1. Replace `<p>` tags with `<br />` 2. Strip all occurances of `\n`

  • template
  • tag
Read More

Caching XHTML render_to_response

This code improves on Django's render_to_response shortcut function in the following ways: 1. If the caller does not provide a MIME type, and the caller is passing a RequestContext, it interrogates the request to determine if the HTTP client supports application/xhtml+xml encoding. If so, it sets the request type to application/xhtml+xml to override the HttpRequest class's default mime type of text/html. 2. It caches parsed templates in its own cache to improve performance. If you aren't using XHTML in your templates, you may choose to comment out the code that tests for and sets the application/xhtml+xml MIME type. I place this code in a file named "util.py" in my application. In my views, I write "from app.util import render_to_response" where I used to write "from django.shortcuts import render_to_response". Note that the caching functionality provided by this code means that you will need to recycle your Django instance when you make template changes. Instructions for doing this depend on how you have deployed your Django application.

  • render_to_response
  • template
  • cache
  • html
  • xhtml
Read More

Template tag: split list to n sublists

Based on [this snippet](http://www.djangosnippets.org/snippets/660/), adapted to split a list into *n* number of sublists, e.g. split a list of results into three evenly-divided sublists to enable display of the results in three columns on one page with CSS. Tag: `{% list_to_columns your_list as new_list number_of_columns %}` The split_seq solution comes from a comment by Sebastian Hempel on [this post](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425397). More info [here](http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/).

  • template
  • list
  • template-tags
Read More

Allow template tags in a Flatpage's content

** This tag, as shown, can cause problems (infinite recursion being one) if you don't use it correctly. ** Our internal CMS has a pages app similar to Flatpages, and a "chunks" app similar to [django-chunks](http://blog.clintecker.com/2008/jul/6/django-chunks/). Because most of our other apps are template-tag driven (FAQs, job postings, news, events, etc) I wanted a way to be able to write django template code right into a page or chunk's body from within the django admin. This tag will let you write django template code into, for example, a Flatpage's content and render it with the current context. So if you had a template tag called "get_latest_news" and wanted to add latest news to a flatpage, just enter this in the flatpage's content: {% get_latest_news 5 as news %} {% for article in news %} <li>{{ article.title }}</li> {% endfor %} This ability has proven extremely useful to us. Please note this is just a "summary" snippet to illustrate the concept. Use in a production environment should include more robust error checking.

  • template
  • flatpages
Read More

YouTube Template Filter

Given a youtube url (can copy and paste right from the browser) turns the url into an embedded video. If you put this in your app's templatetags/filters.py, you can do the following >{{ object.youtube_url|youtube }} and it would embed the youtube video.

  • template
  • filter
  • youtube
  • embed
Read More

Last.fm Charts

This will fetch the Top Artists List for the given username from Last.fm. It makes use of the django.cache Framework. I use it with django 0.96.2. Enjoy! Usage: ` {% load lastfm %} {% lastfm_topartists YourName as topartists %} {% for artist in topartists %} {{ artist.thumbnail }}, {{ artist.name }}, {{ artist.url }}, {{ artist.playcount }} and so on {% endfor %} `

  • template
  • chart
  • lastfm
Read More

BBCode templatefilter (generate & strip)

Parse a string for [BBCode](http://en.wikipedia.org/wiki/Bbcode) and generate it to (X)HTML by using the [postmarkup libary](http://code.google.com/p/postmarkup/). In your template for generating (X)HTML: {% load bbcode %} {{ value|bbcode }} In your template for removing BBCode fragments: {% load bbcode %} {{ value|strip_bbcode }}

  • template
  • templatetag
  • templatefilter
  • bbcode
  • xhmtl
Read More
Author: b23
  • 2
  • 8

262 snippets posted so far.