Commits

janbrohl  committed 0120e2b

mehr namespace für die templates

  • Participants
  • Parent commits e35d510

Comments (0)

Files changed (1)

 from simpletal.simpleTALES import Context
 from urllib import urlopen
 from cStringIO import StringIO
+from simplerest.util import NoCallWrapper, TALVirtualPath
+from urlparse import urlsplit, urlunsplit
 
 template_pfad = "http://localhost/templates4scoutnet/templates/" # oder sowas in der art
 
     charset = request.charset or "UTF-8"
     args = request.args
     template_url = args.get("template", None)
-    if template_url and not template_url.startswith("http"):
-        template_url = template_pfad + "/".join(p for p in template_url.split("/") if p and p != "..")
-    root_url = args.get("root", "")
-    if root_url:
-        pfad = list(p for p in root_url.strip("/").split("/") if p and p != "..")
+    if template_url:
+        tu_parts = urlsplit(template_url)
+        if tu_parts.scheme:
+            pp = urlunsplit(tu_parts[:2] + ([""] * 3))
+        else:
+            pp = template_pfad
+        bp = tu_parts[2].strip("/")
+        if bp:
+            bp = bp.split("/")
+            path = bp.pop(-1) + tu_parts[3]
+        else:
+            bp = []
+            path = tu_parts[3]
+        tvp = TemplateVirtualPath(path=[], basepath=bp, path_prefix=pp)
+        template = tvp[path]()
+        wrapper = TemplateWrapper(tvp)
     else:
-        pfad = []
-    l = LazyWrapper(Lazy(path=pfad))
+        wrapper = None
+        f = open("default.pt")
+        incharset = "UTF-8"
+        template = compileHTMLTemplate(f, incharset)
+        f.close()
+    ctx = NoCallWrapper(Lazy(path=args["context"]))
     d = dict(args.to_dict(False))
-    d["context"] = l
-    s = render(template_url, d, charset)
+    s = render(template, d, ctx, wrapper, charset)
     return Response(s, content_type="text/html;charset=" + charset)
 
+class TemplateVirtualPath(TALVirtualPath):
+    def __call__(self):
+        f = urlopen(self.kwarg("path_prefix") + self.path_as_list)
+        incharset = f.info().getparam("charset")
+        return compileHTMLTemplate(f, incharset)
 
-class LazyWrapper(object):
-    def __init__(self, lazy):
-        self ._l = lazy
+class TemplateWrapper(NoCallWrapper):
+    def macros(self):
+        return self().macros
 
-    def __getitem__(self, key):
-        return LazyWrapper(self._l[key])
-
-    def __str__(self):
-        return str(self._l)
-
-
-def render(url, options, charset):
-    if url:
-        f = urlopen(url)
-        incharset = f.info().getparam("charset")
-    else:
-        f = open("default.pt")
-        incharset = "UTF-8"
-
-    template = compileHTMLTemplate(f, incharset)
+def render(template, options, context, container, charset):
     ctx = Context(options)
+    ctx.addGlobal("context", context)
+    ctx.addGlobal("container", container)
     f = StringIO()
     template.expand(ctx, f, charset)
     return f.getvalue()