Login

Tag "block"

Snippet List

whitespaceoptimize block tag

This is a custom block tag and is used like this: {% load whitespaceoptimize %} {% whitespaceoptimize "css" %} /* CSS comment */ body { color: #CCCCCC; } {% endwhitespaceoptimize %} And when rendered you get this output: body{color:#CCC} To install it, download the snippet and call it `myapp/templatetags/whitespaceoptimize.py`. (Make sure you have a `__init__.py` in the `templatetags` directory) You need to download and install [slimmer](http://www.issuetrackerproduct.com/Download#slimmer) and put on your Python path. The block tag can be used for `javascript` and `html` as well as `css`. You can also let it guess the format; this would work for example: {% whitespaceoptimize %} <table> <tr> ... {% endwhitespaceoptimize %} ...but this would just replicate the functonality of the [built-in spaceless tag](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#spaceless)

  • tag
  • block
  • whitespace
  • slimmer
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

Render specific blocks from templates (useful for AJAX)

Allows getting the rendered content of a specific block tag. Useful if you want to send just a part of a template back for an AJAX request. Works for arbitrary template inheritance, even if a block is defined in the child template but not in the parent. Example: In `test1.html`: {% block block1 %}block1 from test1{% endblock %} {% block block2 %}block2 from test1{% endblock %} In `test2.html`: {% extends 'test1.html' %} {% block block1 %}block1 from test1{% endblock %} And from the Python shell: >>> from django.template import loader, Context >>> from template import render_block_to_string >>> print render_block_to_string('test2.html', 'block1', Context({})) u'block1 from test2' >>> print render_block_to_string('test2.html', 'block2', Context({})) u'block2 from test1' UPDATE: See also [zbyte64](http://www.djangosnippets.org/users/zbyte64/)'s implementation in snippet [#942](http://www.djangosnippets.org/snippets/942/)

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

blocksetter

Sometimes you need to set a little bit more complex variable in the template (e.g. Title), that is being used more than once. this simple tag defines "blockset" tag. {% blockset variable-name %}{%endblockset} everything inside body (between blockset/endblockset) is being assigned to the variable "variable-name". I have found this quite useful with translations, or setting the title, where you out several variables into one sentence. Drop me an email if you will find this useful.

  • templatetag
  • block
  • set
Read More

Line & paragraph chopping

I was faced with the fact that I wanted to post 2 paragraph-long summaries on one of my sites, and this is what I did (you could of course cut it down earlier, but I'd say this belongs to what is called "template logic") Use like so: {% load myExtraModule %} {{ blogpost.content|paragraphs:"2" }} The lines filter works the exact same way, and you might want to improve on these a bit, I don't maintain them as I don't use them anymore.

  • chop
  • cut
  • line
  • paragraph
  • block
Read More

5 snippets posted so far.