Home > Main > Thousand ways to make your life harder

Thousand ways to make your life harder

Just a couple hours out of Lisp world I begin to miss things. It’s a bit paradox, since I didn’t even really use Lisp or any its libraries, but I’ve learned enough about them (and I still know nearly nothing!) to miss features and in some cases simplicity and expressiveness.

Right now, I’m equally excited about learning Pylons and frustrated about so-called template engines. In some weird way, PHP is a template engine too, so I’ll take it as an example to support my point.

Web development could be easily described as masochism since the amount of things you need to know for basic development is enormous. You can’t just go with HTML, CSS, JavaScript (and some theory on HTTP and browser implementations with regard to caching), they are just the basics which could be enough for a simple “home page”, but you’d need a server-side programming language to do some data-crunching.

This language of choice would produce HTML for user’s browser to render, filling pre-defined fields with data, so basically, you’d be converting some HTML template into pure HTML containing your data using your programming language. If you make this language simple enough for small manipulations, you’d get SSI, or you could also make it a full-blown language, in which case you get PHP. Depending on the drugs you take (or not), you might also end up with ASP, ColdFusion or even XSLT but the main principle stays the same and it’s not the point here (this is not a PHP rant ;))

Now the one thing I don’t understand: every single Python templating engine (with one brave exception) uses a PHP-like embedded syntax which is “almost, but not quite, entirely unlike” Python (thanks to Douglas Adams for the excellent expression). It includes things like this:

1
2
3
  % if action == "list":
    <% return '' %>
  % endif

So I’m supposed to learn yet another language with slight deviations to some other language I’m writing the rest of my project in. Seriously, this sucks and it’s highly error-prone. Not to mention the negative karma I’d be producing closing each and every single HTML tag in the template.

Yes, this is still a lot better than writing HTML by hand in PHP (that is, without any frameworks) or in Perl. But this is still a way to make programmer’s job harder and less fun. I’d like to quote Slava Akhmechet on this occasion:

I like HTML in the same way I like PDF - as a document serialization format that does a reasonable job and that I never want to modify by hand. This is one of the main reasons I started Weblocks framework - I never wanted to write a line of HTML again. Ironically, I ended up writing a lot of HTML and learning more about its quirks, accessibility issues, and CSS hooks than I ever wanted to, but I finally ended up with a high level user interface definition language embedded into Common Lisp. Finally, HTML is out of my life, for good. Being lazy does pay off.

Writing HTML is tedious and Slava is right there saying that HTML should be treated like some binary format and not written by hand. It’s entirely possible that it’ll become something like assembly language for the web in a couple of years and only a small group of people will still have to write it manually. We are not there yet, but we could at least make sure that writing HTML happens painlessly, and by “painless” I mean “in the currently used language”.

Common Lisp programmers mostly use CL-WHO for this, which is designed to help the programmer and using Lisp constructs inside CL-WHO expressions makes their life even easier:

1
2
3
4
5
6
7
8
9
10
11
(with-html-output (*http-stream*)
  (:table :border 0 :cellpadding 4
   (loop for i below 25 by 5
         do (htm
             (:tr :align "right"
              (loop for j from i below (+ i 5)
                    do (htm
                        (:td :bgcolor (if (oddp j)
                                        "pink"
                                        "green")
                             (fmt "~@R" (1+ j))))))))))

This is expressiveness I wish to have, this is what I’ve been missing about an hour ago. Luckily, there is one exception in the world of Python template engines, which looks almost like CL-WHO and it’s called Brevé. Even its syntax is like Lisp and it looks like this (more complicated example including inheritance):

1
2
3
4
5
6
7
8
9
10
11
inherits ( 'index' ) [
    override ( 'content' ) [
        h1 ( class_="main" ) [ c.title ],
        p [
            'This page doesn't exist yet.',
            a ( href=h.url_for ( action='
edit', title=c.title ) [
                '
Create the page.'
            ]
        ]
    ]
]

Well, even now I’d still have to know a bit about HTML, but that’s necessary evil right now and I can at least program everything in Python syntax. Not having to enter closing tags by hand could be one single killer feature in Brevé for me. It will (hopefully) make my life easier and that will account for a lot of motivation.

  1. April 19th, 2008 at 08:16 | #1

    Glad you like Brevé! Be sure to check out 1.2 which adds a few features a Lisp fan will like, notably macros:

    http://breve.twisty-industries.com/documentation#macros http://breve.twisty-industries.com/snippets/macro-madness

    I doubt Brevé’s macros are nearly as powerful as Lisp’s, but they are hella more powerful than C’s =)

  1. No trackbacks yet.