Looking for a more efficient way to call a class within a template

4 views
Skip to first unread message

cd34

unread,
Sep 21, 2010, 12:08:27 PM9/21/10
to Mako Templates for Python
I have a template which calls a function by the name stored in the
database. Is there a more efficient way of doing this:

<% output = getattr(h.CMS(), element.function)() %>
${output}

Michael Bayer

unread,
Sep 21, 2010, 12:51:21 PM9/21/10
to mako-d...@googlegroups.com
yeah why aren't you able to say ${h.CMS.do(element)}

?

> --
> You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
> To post to this group, send email to mako-d...@googlegroups.com.
> To unsubscribe from this group, send email to mako-discuss...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.
>

cd34

unread,
Sep 21, 2010, 2:43:34 PM9/21/10
to Mako Templates for Python
I might be missing something somewhere:

models/cms.py:

class Element(Base):
__tablename__ = 'm_element'

id = Column(mysql.BIGINT(20, unsigned=True), primary_key=True,
autoincrement=True)
function = Column(Unicode(80))

lib/cms.py:

class CMS(object):
def article(self):
return render('element_article.mako')

So, Element.function = 'article' in this case, which required the
getattr since the function name is stored in a string.

${getattr(h.CMS(), element.function)()}

works, which removes the two lines. I'm not sure why I didn't see
that before. Too many hours staring at the code I guess. :)
Reply all
Reply to author
Forward
0 new messages