Wednesday, July 13, 2011

Tuesday, July 12, 2011

Been browsing some interesting discussion over at Quora on how they built their site.

It's basically Python and Pylons. But this is cool. They don't use a templating language.

Here's developer Charlie Cheever :

What "templating" means to most people is a way of having the developer write out HTML basically the way that you would send it to the browser and then having a way to include a few things -- typically variable substitution by using special tags lik <% ... %> or similar.

In our case, no one writes any code that looks like HTML/XML literals, so there's nothing in our codebase that really matches what most people think of as templates. We do have view code but that interleaves calls into the model and application logic along with a Python code description of what the HTML for that component should be, which is different from templates which are usually based around the ideas of separating logic and data fetching from this.


This sounds like an approach I've been favouring for a while. I did it in Mind Traffic Control, some other unreleased SdiDesk in Python experiments, and I do it in some Javascript I've written. People think that you should separate HTML from code because HTML is the domain of designers and code is for programmers. But I think HTML is the realm of data-structure (designers should stick to CSS) and part of the programmers' remit.

The way a programmer (or at least, this programmer) wants to express complex data structures is with function composition. So here's an example of my html.py file.


# HTML library
# basic level

def tag(name,x,*argv) :
if x is None :
return u"<"+name+u"/>"
if argv != (None,) :
inside = u''.join(argv)
else :
inside = u''
if isinstance(x,dict) :
# we're passing a dictionary of attributes for the tag
s = u"<%s " % name
s = s + ' '.join(['%s="%s"'%(k,v) for (k,v) in x.iteritems()])
s = s + u">"+inside+u"</"+name+u">"
return s

# or there are no attributes, just inner
return u"<"+name+u">"+x+inside+u"</"+name+u">"


# Now we'll actually make some tags
tags = ['html','head','body','script','p','div','table',
'tr','th','td','ul','ol','li','dt','dd','h1','h2',
'h3','h4','h5','h6', 'style','pre']

loc = locals()
def setit(loc,t) :
loc[t] = lambda x=None,*argv : tag(t,x,*argv)

for t in tags :
setit(loc,t)


# Use like this
html(
head(),
body(
h2("Header"),
p('para1'),
p('para2')
)
)




But I did start to wonder, given the prevalence of templating languages and some of my recent experiences as a Django developer, whether this wasn't just me being wilfully perverse / crazy. I admit I'm kind of relieved to read that Quora are doing something similar. Maybe I wasn't so mad after all.

Bonus link : Decomposition by language is probably a modularity mistake. (Written back when I was more confident.)