Hi
It's going to involve regular expressions. Something I'm not so good at, I've written an example but you (or someone else here) will (I guarantee) have a more efficient way of doing this. But this is just an example:
from django.template import Library, Node, TemplateSyntaxError
from django.template import Template, Context
register = Library()
class TemplateEvalNode(Node):
def __init__(self, value):
self.value = Variable(value)
def render(self, context):
value = self.value.resolve(context)
regex = re.compile('{% \w+ %}')
# Grab the template tag (eg. {% TemplateTag %})
unevaluated_var = regex.search(value)
if unevaluated_var == None:
return value
# Create a template containing only the template tag
t = Template(unevaluated_var)
# Evaluate that template (with context)
return_var = t.render(Context(context))
# Return the original value with the template tag replaced with
# its actual value
return value.replace(unevaluated_var, return_var)
def template_eval(parser, token):
bits = token.contents.split()
if len(bits) != 2:
raise TemplateSyntaxError, "template_eval takes exactly one argument"
return TemplateEvalNode(bits[1])
template_eval = register.tag(template_eval)
I haven't tested this yet, so it may not work straight off, but in theory you can run:
{% template_eval object.textfield %}
And it would return:
a text with a VALUE_OF_TEMPLATETAG.
Expect an update to this as I'm going to test it now and attempt to fix any problems, my battery is about to die so I'm posting this now untested.
Also expect a much more clever solution from someone who is better at Python than I am :p.
EDIT: OK, I was too slow, you beat me!