cheetah templating: place holders needed in a template

Discussion in 'Python' started by ajay.padala@gmail.com, Apr 12, 2006.

  1. Guest

    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
    , Apr 12, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Place holders

    Guest, Nov 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    421
    Chris Jackson
    Nov 13, 2003
  2. Roger Jack

    Cheetah best for templating?

    Roger Jack, Jan 3, 2004, in forum: Python
    Replies:
    5
    Views:
    321
    Ville Vainio
    Jan 5, 2004
  3. Fried Egg
    Replies:
    1
    Views:
    280
    bruno at modulix
    Jan 30, 2006
  4. a
    Replies:
    0
    Views:
    288
  5. Andy B
    Replies:
    0
    Views:
    299
    Andy B
    Jun 15, 2008
Loading...

Share This Page