not good as cheetah.py in webpy

On 6/1/09, Alice Zoë Bevan–McGregor <[email protected]> wrote:
>
> Howdy!
>
> On 1-Jun-09, at 8:37 PM, paul jobs wrote:
>
> does it use cheetah and cheetah.py in a way #includes, #import and #extends
>> work
>>
>
> YAPWF will support (via the "de-facto standard"
> http://docs.turbogears.org/1.0/TemplatePlugins API) Cheetah and #includes,
> #import, and #extends as per the following:
>
> http://docs.turbogears.org/TurboCheetah
> http://docs.turbogears.org/1.0/AdvancedCheetahTemplates
>
>        — Alice.
>
>


-- 
BidEgg - Worlds best low price Auction site
http://bidegg.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

"""
Cheetah API
(from web.py)
"""

__all__ = ["render"]

import re, urlparse, pprint, traceback, sys
from Cheetah.Compiler import Compiler
from Cheetah.Filters import Filter, EncodeUnicode
from utils import re_compile, memoize, dictadd
from net import htmlquote, websafe
from webapi import ctx, header, input, cookies
from web import loadhook

class WebSafe(Filter):
    def filter(self, val, **keywords): 
        return websafe(val)

def upvars(level=2):
    """Guido van Rossum sez: don't use this function."""
    return dictadd(
      sys._getframe(level).f_globals,
      sys._getframe(level).f_locals)

r_include = re_compile(r'(?!\\)#include \"(.*?)\"', re.M)
#r_include = re_compile(r'(?!\\)#include \"(.*?)\"($|#)', re.M)
def __compiletemplate(template, base=None, isString=False):
    if isString: 
        text = template
    else: 
        text = open('templates/'+template).read()
    # implement #include at compile-time
    def do_include(match):
        text = open('templates/'+match.groups()[0]).read()
        return text
    while r_include.findall(text): 
        text = r_include.sub(do_include, text)
    #text = '#encoding UTF-8\n'+text
    execspace = _compiletemplate.bases.copy()
    tmpl_compiler = Compiler(source=text, mainClassName='GenTemplate')
    tmpl_compiler.addImportedVarNames(execspace.keys())
    exec str(tmpl_compiler) in execspace
    if base: 
        _compiletemplate.bases[base] = execspace['GenTemplate']

    return execspace['GenTemplate']

_compiletemplate = memoize(__compiletemplate)
_compiletemplate.bases = {}

def render(template, terms=None, asTemplate=False, base=None, 
           isString=False, filter=EncodeUnicode):
    """
    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') or 
template.endswith('.tmpl')):
    if 'headers' in ctx and not isString and template.endswith('.html'):
        header('Content-Type','text/html; charset=utf-8', unique=True)
        
    #if loadhook.has_key('reloader'):
    #    compiled_tmpl = __compiletemplate(template, base=base, 
isString=isString)
    #else:
    compiled_tmpl = _compiletemplate(template, base=base, isString=isString)
    if filter:
        compiled_tmpl = compiled_tmpl(searchList=terms, filter=filter)
    else:
        compiled_tmpl = compiled_tmpl(searchList=terms)
    if asTemplate: 
        return compiled_tmpl
    else: 
        return str(compiled_tmpl)

Reply via email to