def write(cgi_response): """ Converts a standard CGI-style string response into `header` and `output` calls. """ cgi_response = str(cgi_response) cgi_response.replace('\r\n', '\n') head, body = cgi_response.split('\n\n', 1) lines = head.split('\n') for line in lines: if line.isspace(): continue hdr, value = line.split(":", 1) value = value.strip() if hdr.lower() == "status": web.ctx.status = value else: web.header(hdr, value) web.output(body)
def render(template, terms=None, asTemplate=False, base=None, isString=False): """ Renders a template, caching where it can. `template` is the name of a file containing the a template in the `templates/` folder, unless `isString`, in which case it's the template itself. `terms` is a dictionary used to fill the template. If it's None, then the caller's local variables are used instead, plus context, if it's not already set, is set to `context`. If asTemplate is False, it `output`s the template directly. Otherwise, it returns the template object. If the template is a potential base template (that is, something other templates) can extend, then base should be a string with the name of the template. The template will be cached and made available for future calls to `render`. Requires [Cheetah](http://cheetahtemplate.org/). """ # terms=['var1', 'var2'] means grab those variables if isinstance(terms, list): new = {} old = upvars() for k in terms: new[k] = old[k] terms = new # default: grab all locals elif terms is None: terms = {'context': ctx, 'ctx':ctx} terms.update(sys._getframe(1).f_locals) # terms=d means use d as the searchList if not isinstance(terms, tuple): terms = (terms,) if 'headers' in ctx and not isString and template.endswith('.html'): header('Content-Type','text/html; charset=utf-8', unique=True) if loadhooks.has_key('reloader'): compiled_tmpl = __compiletemplate(template, base=base, isString=isString) else: compiled_tmpl = _compiletemplate(template, base=base, isString=isString) compiled_tmpl = compiled_tmpl(searchList=terms, filter=WebSafe) if asTemplate: return compiled_tmpl else: return output(str(compiled_tmpl))