python - How do I do parsing in Django templates? -
post = { sizes: [ { w:100, title="hello"}, {w:200, title="bye"} ] } suppose pass django templates. now, want display title width = 200. how can that, without doing brute-force way:
{{ post.sizes.1.title }} i want parsed way.
a neat way filter template tag.
from django.template import library register = library() @register.filter('titleofwidth') def titleofwidth(post, width): """ title of given width of post. sample usage: {{ post|titleofwidth:200 }} """ in post['sizes']: if i['w'] == width: return i['title'] return none that should go in templatetags package, postfilters.py, , {% load postfilters %} in template.
naturally alter give correct sizes object , {% post|detailsofwidth:200 postdetails %}{{ postdetails.something }}, {{ postdetails.title }}{% endwith %}.
Comments
Post a Comment