tags:

views:

148

answers:

2

I use the built in 'sum' function in a web.py templator template and I get the following error:

global name 'sum' is not defined

Source code is below:

$if profs:
   $for prof in profs:
      $sum([1, 2, 3])

I can use 'sum' just fine at a Python REPL in the terminal.

What could be the issue?

Thanks, Jacob

A: 

Not all python code is available in template notation, try something like this:

$if profs:
   $for prof in profs:
      $code:
         mysum = sum([1, 2, 3])
      $mysum
truppo
Nope, error persists. I just want to sum a list, I would figure that would be part of the Python subset they would allow in templates, but maybe I am wrong.
Jacob Lyles
Template languages usually don't allow for direct instantiate of function calls. You might have to do something to assign the `sum` value ahead of time, such as by way of an attribute or a method.
jathanism
A: 

Add the functions in a dict and pass as the globals argument to render:

render = web.template.render('templates/', globals={'sum': sum})

Then in your template you can just use it:

$def with (numbers)

<h1>Numbers add to $sum(numbers)</h1>
davidavr