2

I just started to play around with cherrypy and wanted to use cheetah as a templating engine.

Therefore I wanted to create a tool so I just can use the annotation feature to point to my template

something like

import cherrypy


class Root(object):
    @cherrypy.expose()
    @cherrypy.tools.cheetah(template="index")
    def index(self):
        title = "Demo"
        content = "Stuff"
        return {'title': title, 'content': content}

I already found something on the cherrypywiki that would work with a compiled template:

CherryPy Cheeta Tool

But I don't want to compile the template first. I want to return my content from my exposed site. My cheetah tool should now intercept that content and create the template

I know how I can create the template:

from Cheetah.Template import Template
....
 cherrypy.expose()
 def demo(self):
    filename = os.path.join(APPDIR, "index.tmpl")
    template = Template(file = filename)

    template.content = "bla"
    template.title = "Test"
    return str(template)
 ....

Now basically in my page handler I just return a dictionary of my content and my tool creates the template and fills the attributes dynamically. Since I'm also new to Python I don't know how to do this dynamically.

I was hoping I could iterate through my dictionaries and do something like that within my tool:

template = Template(file = filename)
for key, value in data:
    setattr(template, key, value)

But I already tried this with a short demo. setattr is not working. I tried it like this:

template = Template(file = filename)
for key, value in data:
    setattr(template, 'title', 'Test')

Could someone point me please into the right direction?

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
daniel
  • 1,002
  • 13
  • 22
  • possible duplicate of [CherryPy with Cheetah as plugin + tool - blank pages](http://stackoverflow.com/questions/24578794/cherrypy-with-cheetah-as-plugin-tool-blank-pages) – Mez Jul 08 '14 at 12:18

0 Answers0