cheetah templating: place holders needed in a template

ajay.padala at gmail.com ajay.padala at gmail.com
Wed Apr 12 13:31:52 EDT 2006


Hi

I would like to be able to get the inputs that are needed into a
template file programmatically.

For example if we had a template:

=====================
$name has $num marbles in his hand.
=====================

I want to be able to initialise this template and call a method on it
to get the list ['name', 'num']

Is there anything in cheetah which does this?

At present, I am sending a "fake" dictionary into the searchList to do
this. This is coded as.

    fake = Generic()
    t = Template(file=absFileName, searchList=[fake])
    print fake.calledAtts

=========================================

class Generic(object):
    """ Generic object can pretend to be a dictionary or a list and
doesn't throw
        any errors whatever you call it for """

#    def __new__(cls, *args, **kwargs):
#        if '_inst' not in vars(cls):
#            cls._inst = object.__new__(cls, *args, **kwargs)
#        return cls._inst

    calledAtts = sets.Set()

    def __init__(self, *args, **kwargs):
        pass

    def __call__(self, *args, **kwargs):
        return self

    def __repr__(self):
        return "Generic"

    def __nonzero__(self):
        return True

    def __getattr__(self, name):
        return self

    __delattr__ = __getattr__

    def __setattr__(self, name, value):
        return self

    def __len__(self):
        return 1

    def __iter__(self):
        return iter(("Generic",))

    def __getitem__(self, i):
        if isinstance(i,types.StringType):
            self.calledAtts.add(str(i))
        return self

    def __setitem__(self, i, v):
        return self

    def __contains__(self, itemName):
        print "contains " + itemName
        return True

    def has_key(self, *args, **kwargs):
        # print "has key called"
        return True

    __delitem__ = __getitem__

========================================

This just logs each access to the dictionary and stores the key under
self.calledAtts

But obviously this won't work if I have #if statements etc. I am hoping
cheetah already has this feature somewhere which is much more robust
but I can't find it.

Ajay




More information about the Python-list mailing list