[Grok-dev] Anyone using Cheetah Template Engine with Grok?

Paul Wilson paulalexwilson at gmail.com
Mon Jan 5 18:58:26 EST 2009


2009/1/5 Brian Wolf <brian.wolf at activustech.com>:
> Is anyone using Cheetah Template Engine with Grok?
> I'm having difficulty configuring it to run with Grok.  File meta.zcml
> is not found during start-up because, well, it was not created when
> buildout was run.  If it must be hand-created, what is the correct
> content and format?

Are you trying to incorporate cheetah as a separate package that plugs
into your project and allows for cheetah templates to be used
automatically like megrok.genshi? Or are you just trying to get
Cheetah working within a project your working on? If it's number 2,
then...

I've had success with an app.py that looks like this:

import grok
import os
import grokcore
from Cheetah.Template import Template

class Testcheetah(grok.Application, grok.Container):
    pass

class Index(grok.View):
    pass # see app_templates/index.pt

class TestCheetahTemplate(grok.View):
    def namespace(self):
        return {'place':'World!!'}

class CheetahTemplate(grokcore.view.components.GrokTemplate):
    def setFromString(self, string):
        self._template = Template.compile(string)

    def setFromFilename(self, filename, _prefix=None):
        file = open(os.path.join(_prefix, filename))
        self._template = Template.compile(file=file)

    def render(self, view):
        return str(self._template(namespaces=self.getNamespace(view)))

class CheetahTemplateFactory(grok.GlobalUtility):
    grok.implements(grok.interfaces.ITemplateFileFactory)
    grok.name('ch')

    def __call__(self, filename, _prefix=None):
        return CheetahTemplate(filename=filename, _prefix=_prefix)

We need to let our project know that we need Cheetah so our
install_requires in setup.py now looks like:

install_requires=['setuptools',
                  'grok',
                  'grokui.admin',
                  'z3c.testsetup',
                  'Cheetah',
                  ...

Also, let buildout know about the change:...

bin/buildout...
...
Getting distribution for 'Cheetah'.
Got Cheetah 2.0.1.
...

Now we need to also create a Cheetah template too called
testcheetahtemplate.ch to match the name of our view and the name we
defined in the cheetah template factory (it could look something like
this):

         Hello Cruel $place

$place matches the namespace variable added to the TestCheetahTemplate
view's namespace.

If you've called your app 'test' you'll be able to see the templated file at:

        http://localhost/test/testcheetahtemplate

Note that you may at one point want to reference the static directory
of your project, you can do this with:

        self.static['images']['my_image.png']()

assuming the directory layout of

        static/images/my_image.png

I have trouble locating this information in the docs...

This is just hacked together quickly with little testing, but should be a start!

Hope that helps.

Regards,
Paul


More information about the Grok-dev mailing list