Again, I'm definitely not an expert, but it sounds to me like you're passing globals
when you want to actually render the string. From their tutorial it looks like they expect you to create a global instance of the renderer and pass globals
at that point, so it's a fixed mapping for all template renders. Then you call the appropriate method on that instance to render each template - to this you don't need to pass the template directory each time, just the parameters for the template.
For example, I created this template as templates/hello.html
:
$def with (name)
$code:
value = 123
title = "Easy as " + str(value)
<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<p>Hello, $name</p>
</body>
</html>
And then I used the following Python session to create a renderer and use it, first without passing a globals
(hence causing an exception due to a lack of str
) and then with the appropriate globals
:
>>> import web
>>> render = web.template.render("templates")
>>> print render.hello("world")
Traceback (most recent call last):
[... traceback omitted for clarify ...]
NameError: global name 'str' is not defined
>>>
>>> render = web.template.render("templates", globals={"str": str})
>>> print render.hello("world")
<html>
<head>
<title>Easy as 123</title>
</head>
<body>
<h1>Easy as 123</h1>
<p>Hello, world</p>
</body>
</html>
In your case, you'll presumably create the render
instance globally, or in some other static storage, and pass both the templates directory and the globals
to it on construction. The crucial point here is that you do this once when your web app is initialised, and not for every request. Then to generate your HTTP responses, you'll call the template methods on this static instance as I've done above with my hello.html
template.
Does that help?