Olivier Favre-Simon wrote:
> (http://groups.google.com/groups?q=cherrypy yields only cherrypy-users
> and cherrypy-devel for now, not yet cherrypy-docs, so I CC to
> cherrypy-users.)
>
>
> Today I stumbled across TurboStan (http://blog.develix.com/), a template
> engine plugin for TurboGears.
>
> Here is a shameless rip of the TurboStan example, adapted for CherryPy
> alone.
>
>
>
> The same way I'm falling in love for cool JSON data instead of
> heavyweight XML, I like Stan's simplicity.
>
> I've not done any benchmarking but it must render ultra-fast because
> almost all template engines are built around this design: generate an
> intermediate python DOM. With Stan you _start_ from a python DOM.
>
>
>
> I've attached files cherrypy_stan_template.py and index.stan but below
> is inlined source:
>
>
> Comments/corrections are welcomed :-)
>
>
>
> #!/usr/bin/env python
>
> ################################################################################
> #
> # Name: cherrypy_stan_template.py
> #
> # Description : Example of using Nevow's Stan templates
> #
> # Shameless rip from the TurboStan plugin for TurboGears, written by
> # Cliff Wells (http://blog.develix.com/)
> #
> # A good overview of Stan is "Meet Stan: The Nevow Document Object Model"
> # at http://www.kieranholland.com/prose/meet-stan/
> #
> # Revision: 1.0
> #
> # Date: 2006-01-03
> #
> # CherryPy compatibility: >= 2.1
> #
> # Required modules: nevow
> #
> # Author: Olivier Favre-Simon (olivier.f...@club-internet.fr
> #
> ################################################################################
>
>
> __author__ = "ofs"
> __revision__ = "1.0"
>
> import cherrypy
>
> ### FIXME: CP (as of r908) chokes if I put these imports here...
> #from nevow.flat import flatten
> #from nevow.tags import *
>
>
> # Create a template file named 'index.stan' in server root directory,
> # with same content as in docstring below.
> #
> # The template engine will try to match all symbols in the DOM.
> #
> # html, head, body, etc. are symbols from nevow.tags.
> #
> # Here we use vars.title and vars.renderText as content placeholders.
> #
> """
> html [
> head [
> title [vars.title],
> ],
>
> body [
>
> div (style = 'color: blue;') [
>
> a (href = 'http://www.cherrypy.org') [
> img (src = 'http://www.cherrypy.org/trac/cherryPyLogo.gif')
> ],
>
> h1 [vars.title],
>
> p(style='font-family: Verdana;')[ "Here we're inside the
> blue div" ],
>
> span (render = vars.renderText)
> ]
> ]
> ]
> """
>
> class Root:
>
> @cherrypy.expose
> def index(self):
> return self.render('index.stan')
>
> def render(self, template):
>
> # Will get a "SyntaxWarning: import * only allowed at module
> level" but see comment at the top
> from nevow.flat import flatten
> from nevow.tags import *
>
> vars = self.Vars({'title':'CherryPy with Stan template :-)',
> 'renderText':self.renderText})
>
> dom = eval(file(template).read())
>
> return flatten(dom)
>
>
> def renderText(self, context, data):
> return context.tag(style = 'color: red;')[ 'This text is
> dynamically rendered (always inside the blue div, but color changed to
> red)' ]
>
>
> class Vars:
> '''
> Turn a dict into a class for notational convenience in templates.
> '''
> def __init__(self, values):
> self.__dict__.update(values)
>
>
> cherrypy.root = Root()
>
> cherrypy.server.start()
>
>
>