views:

590

answers:

2

I love the StringTemplate engine, and I love the CherryPy web server, and I know that they can be integrated.

Who has done it? How?

EDIT: The TurboGears framework takes the CherryPy web server and bundles other related components such as a template engine, data access tools, JavaScript kit, etc. I am interested in MochiKit, demand CherryPy, but I don't want any other template engine than StringTemplate (architecture is critical--I don't want another broken/bad template engine).

Therefore, it would be acceptable to answer this question by addressing how to integrate StringTemplate with TurboGears.

It may also be acceptable to answer this question by addressing how to use CherryPy and StringTemplate in the Google App Engine.

Thanks.

A: 

Rob,

There's reason behind people's selection of tools. StringTemplate is not terribly popular for Python, there are templating engines that are much better supported and with a much wider audience. If you don't like Kid, there's also Django's templating, Jinja, Cheetah and others. Perhaps you can find in one of them the features you like so much in StringTemplate and live happily ever after.

Eli Bendersky
People are notorious for selecting bad X, including templating engines, so I am not interested in a popularity contest. I want a GOOD template engine, which means enforcement of MVC-like separation, which has only one option: StringTemplate. No one else has bothered.
Rob Williams
+1 to cancel the downvote. StringTemplate has awkward if-then statements and no for loops at all...and no attribute access on variables...surely that's a little draconian, if they count as program logic?
Nikhil Chelliah
Eli: You noted that "there's reason [in] selection of tools." However, the reasons I infer from your argument seem guided by concerns Rob doesn't share. StringTemplate's designer, Terence Parr, has made text manipulation his life's work (literally.) I encourage you to read the papers explaining ST's philosophy:"A Functional Language For Generating Structured Text" (http://www.cs.usfca.edu/~parrt/papers/ST.pdf) and "Enforcing Strict Model-View Separation in Template Engines" (http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf)Like Python's whitespace, I once thought his ideas nuts...
Tripp Lilley
Nikhil: See http://www.antlr.org/wiki/display/ST/Five+minute+Introduction . ST allows references to properties of an object in the template namespace, e.g., <user.name>. Looping comes the form of a map()-like functional iteration over a multi-valued attribute (a list, dict, etc.,) e.g., <users:{s|<li>$s$</li>}; separator="\n"> I also don't agree with you about the "awkward if-then statements." This looks about as normal a template if/then as I've seen: <if(users)> Yes, there are users! <endif>
Tripp Lilley
+4  A: 

Based on the tutorials for both, it looks pretty straightforward:

import stringtemplate
import cherrypy

class HelloWorld(object):
    def index(self):
        hello = stringtemplate.StringTemplate("Hello, $name$")
        hello["name"] = "World"
        return str(hello)
    index.exposed = True

cherrypy.quickstart(HelloWorld())

You'll probably want to have the CherryPy functions find the StringTemplate's in some location on disk instead, but the general idea will be like this.

Django is conceptually similar: url's are mapped to python functions, and the python functions generally build up a context dictionary, render a template with that context object, and return the result.