Login

{% eval %} templatetag-evaluate expressions

Author:
diverman
Posted:
November 27, 2009
Language:
Python
Version:
1.1
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. codigo alto nivel by MrRocklion 1 week, 1 day ago
  2. Load template from specific app by Krzysiek555 4 weeks ago
  3. PostgreSQL JSON subqueries by dolamroth 1 month ago
  4. "Magic Link" Management Command by webology 6 months ago
  5. Closest ORM models to a latitude/longitude point by simonw 6 months ago

Comments

Please login first before commenting.