Skip to content

Instantly share code, notes, and snippets.

@yuchant
Created February 28, 2012 00:15
Show Gist options
  • Save yuchant/1928058 to your computer and use it in GitHub Desktop.
Save yuchant/1928058 to your computer and use it in GitHub Desktop.
Django resolve variable with variable value tag
"""
Resolve lookup based on variable value
--------------------------------------
{% resolve foovar foodict %} # does django's Variable(foovar).resolve(foodict)
Created on Monday, February 2012 by Yuji Tomita
"""
from django import template
from django.template.base import Variable, VariableDoesNotExist
register = template.Library()
@register.assignment_tag()
def resolve(lookup, target):
try:
return Variable(lookup).resolve(target)
except VariableDoesNotExist:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment