Login

{% eval %} templatetag-evaluate expressions

Author:
diverman
Posted:
November 27, 2009
Language:
Python
Version:
1.1
Tags:
template templatetag exec eval
Score:
1 (after 1 ratings)

This templatetag evaluates Python expressions including built-in functions, lambda functions etc... Use with caution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django import template

register = template.Library()

@register.tag(name='eval')
def do_eval(parser, token):
    "Usage: {% eval %}1 + 1{% endeval %}"

    nodelist = parser.parse(('endeval',))

    class EvalNode(template.Node):
        def render(self, context):
            return eval(nodelist.render(context))
 
    parser.delete_first_token()
    return EvalNode()

More like this

  1. FloatField with safe expression parsing by joelegner 5 years, 2 months ago
  2. math tag by itchyfingrs 4 years ago
  3. A templatetag to insert the output of another view (or local URL) by jamesgpearce 5 years, 11 months ago
  4. lazy url reverse() by guettli 7 years, 5 months ago
  5. Automatic Paragraphs by Mirrorball 7 years, 9 months ago

Comments

Please login first before commenting.