Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 2 (modified by sean@…, 13 years ago) (diff)

added a comment

This is a clever idea I had that maybe someone else could find usefull

I'll like to thank David Stanek from the Kid discuss list that help me out, when my call to locals() recurse to infinity

So I wanted a kid template that will display any variable that I send to it, so later on a designer or myself will replace it with the real template.

so here it is.

def template_locals(template):
   for name, value in template.__dict__.iteritems():
        if not name.startswith('_') and not name.startswith('tg') and not name is "std":
            yield name, repr(value)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
        <title>Welcome to TurboGears</title>
        <li py:for="var,val in template_locals(self)">
           <span>${var} = ${val}</span>

please note that the template_locals function maybe filter out whatever you like all that needs to be done is add more lines to that if.


Just a small note. You shouldn't do identity tests on strings, its only the fact that small strings are (by convention) cached, that allows an is test to succeed in this case. So and not name is "std" should be and name != "std". (any literal string over a certain number of characters will generate a new object every time, with my version of python 2.4.1 its 9 characters)

-- Sean Jamieson