Skip to content

Commit

Permalink
integrating mako
Browse files Browse the repository at this point in the history
  • Loading branch information
bmuller committed Aug 19, 2010
1 parent c752b6a commit 2e50f21
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions twistler/controllers.py
@@ -1,38 +1,62 @@
from nevow import rend, inevow, static
from twisted.web.util import Redirect

from mako.lookup import TemplateLookup
import os

class AppController(rend.Page):
addSlash = True
controllers = {}

def __init__(self, viewsDir="views", templateCacheDir=None):
"""
Don't cache by default.
"""
# this is the root of our views
self.viewsDir = viewsDir
self.templateDirs = [viewsDir]
self.templateCacheDir = templateCacheDir
self.addViewDir('layout')
self.controllers = {}


def addViewDir(self, dir):
self.templateDirs.append(os.path.join(self.viewsDir, dir))
self.tlookup = TemplateLookup(directories=self.templateDirs, module_directory=self.templateCacheDir)


@classmethod
def addController(appklass, klass, paths=None):
def addController(self, klass, paths=None, viewDir=None):
"""
viewDir will be relative to the viewsDir param in the constructor.
"""
name = klass.__name__[:-10].lower()
AppController.controllers[name] = klass
self.controllers[name] = klass
paths = paths or []
for path in paths:
AppController.controllers[path] = klass
self.controllers[path] = klass

# Add view directory
viewDir = viewDir or name
self.addViewDir(viewDir)


def locateChild(self, ctx, segments):
if len(segments) == 0:
return rend.NotFound

contname = segments[0]
if not AppController.controllers.has_key(contname):
if not self.controllers.has_key(contname):
return rend.NotFound

contklass = AppController.controllers[contname]
return contklass(ctx, segments)._render()
contklass = self.controllers[contname]
return contklass(ctx, segments, self)._render()



class BaseController:
def __init__(self, ctx, segments):
def __init__(self, ctx, segments, tlookup):
self.ctx = ctx
self.segments = segments
self.name = segments[0]
self.tlookup = tlookup

self.action = 'index'
if len(segments) > 1 and segments[1] != '':
Expand All @@ -43,6 +67,12 @@ def __init__(self, ctx, segments):
self.id = segments[2]


def view(self, args, name=None):
name = name or self.action
temp = self.tlookup.get_template("%s.phtml" % name)
return temp.render(**args)


def _render(self):
func = getattr(self, self.action, None)
if func is not None and callable(func):
Expand Down

0 comments on commit 2e50f21

Please sign in to comment.