Django template tag: display some content in specified time range

Django template tags are very powerful tool for extending this template framework, in fact built-in set of filters and tags is very large but from time to time it is the need to do something custom. 
Recently I have got a task that required to display some content in specified time range. I could not find built in solution so I have prepared template tag that behave like {% if %}{% else %} tag set.

Code is available here: https://github.com/MrTheodor/django-snippets

How to use it?

Usage:
   1. Define in settings dict TIME_BLOCK with names of block that has to be
   shown on certain time:
       TIME_BLOCK = {
           'block1': {
               'from_date': datetime.datetime(2012, 04, 28, 13, 48),
               'to_date': datetime.datetime(2012, 04, 30, 13, 48),
               'show': True
           }
       }
   
   2. In template use it in such way:
       
       {% showtime block1 %}
           content that will be displayed between 2012-04-28 13:48 and 2012-04-30
           13:48
       {% else %}
           content that will be displayed before and after that time
       {% endshowtime %}

  There is also the possibility to do it in the reverse way by using option 'show'
 

Enjoy!

No comments: