{% eval %} templatetag-evaluate expressions

 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 4 years, 1 month ago
  2. math tag by itchyfingrs 2 years, 11 months ago
  3. A templatetag to insert the output of another view (or local URL) by jamesgpearce 4 years, 10 months ago
  4. lazy url reverse() by guettli 6 years, 4 months ago
  5. Automatic Paragraphs by Mirrorball 6 years, 8 months ago

Comments

(Forgotten your password?)