2

So I am having some trouble trying to import functions and run them inside my cheetah templates.

So I have one file that lives at /docroot/tmpl/base.html and then another file that is /docroot/tmpl/comments.html

inside of comments I have something that looks like this

#def generateComments($commentObj):
 code for generating comments
#end def

then inside of base.html I want to have a syntax like this

#import docroot.tmpl.comments as comments
<div class="commentlist">
 $comments.generateComments($commentObj)
</div>

However when I run that output I just get the contents of comments.html printed out including the #def generateComments in raw txt.'

What am I missing?

BillPull
  • 6,853
  • 15
  • 60
  • 99

1 Answers1

1

Cheetah compiles templates to Python classes. When you import comments module the module consists of a single class also named comments. You need to explicitly instantiate the class and call its generateComments method. So your code should be

#from docroot.tmpl import comments
<div class="commentlist">
 $comments.comments().generateComments($commentObj)
</div>

The first comments is a module, comments.comments is a template class in the module, comments.comments() is an instance of the class, comments.comments().generateComments($commentObj) is a call to its method. To simplify the code a bit import the class:

#from docroot.tmpl.comments import comments
<div class="commentlist">
 $comments().generateComments($commentObj)
</div>
phd
  • 82,685
  • 13
  • 120
  • 165