Doge log

Abby CTO 雑賀 力王のオフィシャルサイトです

Django meets stan

誰も手を出してない領域。
それがstan。

Since writing this document I have defected. Not to the dark side, but instead to another Python framework: Django. Check it out if you haven't. Nevow implements some really nice ideas and Django doesn't offer anything to compete with LivePage, but when it comes down to capturing precious mindshare, fostering a community, producing quality documentation, and ultimately Getting Things Done, Django is hard to beat. Django components are loosely coupled, so I can even use Stan if I want ;)

http://www.kieranholland.com/code/documentation/nevow-stan/

だそうです。
そう!時代はstanなのです!
stanはhtmlをpythonicに記述できるという代物で変態的。
Nevowで使われるテンプレートのひとつだ。
djangoでも一応使えるのでサンプル。

from django.http import HttpResponse

from twisted.python import components
from nevow import accessors, flat, inevow, tags as T

class User(object):
    def __init__(self, name, user_name):
        self.name = name
        self.user_name = user_name
        self.list = []

components.registerAdapter(accessors.ObjectContainer, User, inevow.IContainer)

def data2string(context, data):
    return context.tag[ str(data) ]

def test_stan1(request, create_user):
    if request.GET.has_key('name'):
        name = request.GET['name']
    else:
        name = "ukuku"
    user = User(name, create_user)
    someStan = T.html[
                T.table(data=user)[
                    T.tr[
                        T.td[ "Name:" ],
                        T.td(data=T.directive('name'), render=data2string)
                    ],
                    T.tr[
                        T.td[ "create_user:" ],
                        T.td(data=T.directive('user_name'), render=data2string)
                    ],
                ]
            ]
    res = HttpResponse(str(flat.flatten(someStan)))
    return res

def test_stan2(request, create_user):
    if request.GET.has_key('name'):
        name = request.GET['name']
    else:
        name = "ukuku"
    someStan = T.html[
                T.h1[ T.slot(name='name') ],
                T.p[ T.slot(name='create_user') ]
            ]
    ctx = {'name':name, 'create_user':create_user}
    for k, v in ctx.items():
        someStan.fillSlots(k, v)
    res = HttpResponse(str(flat.flatten(someStan)))
    return res

ObjectContainer、fillSlotsとかは動作するけどsequenceがうまく動かない。
(Pageに依存してるくさいけど詳しく調べていない)
しょぼいHTMLならstanの方が早く書ける事に気づいた。
あと、Python CookbookにはNevowの事が記述されている。多分、Nevowの事を取り喘げている唯一の書籍だろう。
うくく。