Is there a simple way in Django to render a text field as a template in a template?

Can you imagine an easy way to evaluate a text field as a template during template rendering?

I know how to do this in a view, but am I looking for a template filter or tag?

Something like:

{{ object.textfield|evaluate}} or {% evaluate object.textfield %}

with object.textfield containing something like:

a text with a {% TemplateTag %}.

In which the TemplateTag will be evaluated using the evaluate filter.

+3
filter django tags templates
source share
2 answers

Here is the first tag implementation to solve my question:

 from django import template register = template.Library() @register.tag(name="evaluate") def do_evaluate(parser, token): """ tag usage {% evaluate object.textfield %} """ try: tag_name, variable = token.split_contents() except ValueError: raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0] return EvaluateNode(variable) class EvaluateNode(template.Node): def __init__(self, variable): self.variable = template.Variable(variable) def render(self, context): try: content = self.variable.resolve(context) t = template.Template(content) return t.render(context) except template.VariableDoesNotExist, template.TemplateSyntaxError: return 'Error rendering', self.variable 
+11
source share

It will include regular expressions. Something I'm not so good, I wrote an example, but you (or someone else here) will (I guarantee) have a more efficient way to do 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 have not tested this yet, so it may not work right away, but theoretically you could run:

 {% template_eval object.textfield %} 

And he will return:

 a text with a VALUE_OF_TEMPLATETAG. 

Expect updates, as I'm going to test it now and try to fix any problems, my battery is about to die, so I'm sending it now unverified.

Also expect a much smarter solution from someone who is better in Python than me: p.

EDIT: Alright, I was too slow, you beat me!

+1
source share

All Articles