Has this patch been applied to webpy?

---------- Forwarded message ----------
From: John Matthews <[email protected]>
Date: Apr 28, 2007 1:15 PM
Subject: [webpy] Patch: When using Cheetah allows location of 'templates'
directory to be specified
To: "web.py" <[email protected]>


Below patch allows a baseDir to be specified for each call to
web.render.  I needed this when working with mod_wsgi and cheetah.

Example usage:
templateHome=os.path.join(os.path.dirname(__file__), 'templates')

class index:
   def GET(self):
      web.render("index.html", baseDir=templateHome)

Note: This includes an update to the regular expression which handles
include statements.


Index: cheetah.py
===================================================================
--- cheetah.py  (revision 161)
+++ cheetah.py  (working copy)
@@ -18,15 +18,19 @@
       sys._getframe(level).f_globals,
       sys._getframe(level).f_locals)

-r_include = re_compile(r'(?!\\)#include \"(.*?)\"($|#)', re.M)
-def __compiletemplate(template, base=None, isString=False):
+r_include = re_compile(r'(?!\\)#include \"(.*?)\"([\s]*$|#)', re.M)
+def __compiletemplate(template, base=None, isString=False,
baseDir=None):
+    if not baseDir:
+        baseDir = 'templates/'
+    if baseDir[-1] != '/':
+        baseDir = baseDir + '/'
     if isString:
         text = template
     else:
-        text = open('templates/'+template).read()
+        text = open(baseDir+template).read()
     # implement #include at compile-time
     def do_include(match):
-        text = open('templates/'+match.groups()[0]).read()
+        text = open(baseDir+match.groups()[0]).read()
         return text
     while r_include.findall(text):
         text = r_include.sub(do_include, text)
@@ -44,7 +48,7 @@
  _compiletemplate.bases = {}

  def render(template, terms=None, asTemplate=False, base=None,
-           isString=False):
+           isString=False, baseDir=None):
     """
     Renders a template, caching where it can.

@@ -63,6 +67,9 @@
     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`.

+    'baseDir' optional string, if set it must point to the absolute
path where the
+    template is to be found.
+
     Requires [Cheetah](http://cheetahtemplate.org/).
     """
     # terms=['var1', 'var2'] means grab those variables
@@ -84,9 +91,9 @@
         header('Content-Type','text/html; charset=utf-8',
unique=True)

     if loadhooks.has_key('reloader'):
-        compiled_tmpl = __compiletemplate(template, base=base,
isString=isString)
+        compiled_tmpl = __compiletemplate(template, base=base,
isString=isString, baseDir=baseDir)
     else:
-        compiled_tmpl = _compiletemplate(template, base=base,
isString=isString)
+        compiled_tmpl = _compiletemplate(template, base=base,
isString=isString, baseDir=baseDir)
     compiled_tmpl = compiled_tmpl(searchList=terms, filter=WebSafe)
     if asTemplate:
         return compiled_tmpl





-- 
Bidegg worlds best 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to