5 ms·
Could you reduce the noise by having the accumulator (W in the below) support HTML generation methods? @weby.template() def index(W): with W.ht
by spoondan 16y ago
Could you reduce the noise by having the accumulator (W in the below) support HTML generation methods?
@weby.template()
def index(W):
with W.html():
with W.head():
W.title('Hello, world')
with W.body():
W.h1('Hello, world')
W.p('Lorem ipsum dolor sit amet')
with W.ul():
W.li('Lorem')
W.li('Ipsum')
W.li('Dolor')
with W.div(className='footer'):
W.p('foo bar baz bim')
A smaller win is to use keyword arguments (instead of passing a dict).
- ljlolel 16y agoThose are good ideas, and I experimented with those. I also appreciate that you actually looked into it and thought deeply about how this can be improved. You gain a handful of characters you don't have to type. You lose a lot of flexibility, however. For example, what if you want to use 2 different libraries in a template? Now, you cannot do that since the W accumulator is bound to one of the libraries, or if you do bind multiple ones then they may conflict in odd ways. But maybe that's overstating the problem, worrying too much. Maybe it's okay that within each function we just assert that it can only use one library. I'll think about this more. I'm still a little worried it might be hard to explain. Do you think it'll be easy enough to describe to someone that W is both an accumulator and a library in one? The dict idea would save a few characters, but it leads to problems with, for example, attributes that conflict with other arguments in the function call, or Python keywords. Even in your own example, you put className instead of class because class is a restricted keyword. It is important for me not to have to force the user to have to learn (and remember) that convention. Moreover, in this case of the class attribute, the most common kind actually, it saves no characters.