Show HN: I'm writing a leanpub book about Common Lisp web programming
lispwebtales.ppenev.com
lispwebtales.ppenev.com
(defun blogpage (&optional (posts *posts*))
(html-frame
"Restas Blogdemo"
(<:div
(<:a :href (genurl 'add) "Add a blog post")
(<:hr)
(render-posts posts))))
I don't know much about functional programming though, so sorry if that's an incorrect assumption.Here's a Racket example. If you download http://racket-lang.org, you can just run this (no extra libraries or anything) and a browser window will pop up for you.
#lang web-server/insta
;; A "hello world" web server
(define (start request)
(response/xexpr
'(html
(body (h1 "Hello, world!")
(a ([href "http://google.com"]) "Google")
(br)
"I hope you like my site!"))))
This example renders as:
<html><body><h1>Hello, world!</h1><a href="http://google.com>Google</a><br />I hope you like my site!</body></html>See also: http://docs.racket-lang.org/continue/
In a practical setting you will get a set of pre-cut HTML/CSS and images. Good luck splitting that into semantic markup and styling. And good luck teaching a designer how to work with it afterwards.
Because of hard-earned experience I try very hard not to put any HTML into my code, keeping it all in templates, which are much more accessible to designers and easier to modify.
The choice to use s-expressions was made because It makes it easier to just type code in a repl and experiment, without having to worry about dealing with templates, and also because I wanted the first example to fit in a single file.
If you prefer, Racket also includes a templating library that lets you compile arbitrary HTML from separate files right into your application:
http://docs.racket-lang.org/web-server/templates.html?q=in-t...
It's not as fancy as enlive though (for example, because it's not sensitive to the structure of XML, user input isn't escaped automatically, which is quite an awkward tradeoff)
I keep all my view functions in one .rkt file, and I move all my logic functions to another.
(get "/hello/:person1/:person2" "Hello #{person1} and #{person2}")
http://localhost:8021/hello/Jack/Jill "Hello Jack and Jill"
Much nicer imho than many of the ugly lisp ways. And all in Emacs Lisp.
- Are guaranteed to automatically produce perfect, valid HTML (or XML in my case),
- Automatically escape user-supplied input
- Make it very easy to generate lots of repetitive elements, or conditional elements, like other template languages.
Maybe I'm misunderstanding the spirit of your post? It seems like thinatra excels at the "routing requests from varying URLs" part, which solves a slightly different problem than the code we posted (racket for example provides a default path of http://localhost:40986/servlets/standalone.rkt -- definitely not what you would want)
Also, this has nothing to do with functional programming but rather with creating a DSL (http://en.wikipedia.org/wiki/Domain-specific_language)
More links:
• http://www.tfeb.org/lisp/hax.html#HTOUT
• https://github.com/franzinc/aserve/blob/master/htmlgen/test....
...this way or the Clojure Hiccup way is cool for an ubergeek doing full-stack development, but in the real world you'll want your HTML templates separate and in a designer or front-end-coder friendly format. Otherwise, you may have the full power of Lisp at your fingertips, but you'll need to have your entire team used to this workflow and knowledgeable of Lisp, and by having logic in templates and such you're dangerously close to leaning towards the "PHP way".
...the only realistic "homogenous" solution for one language on the server and the client are the Javascript based way, in the Node ecosystem. Good designers or good front-end guys in general will also have coding skills, some will be Javascript gurus, but by doing things this way and writing Javascript code in Parenscript/Clojure script you'll have to force them to learn Lisp in order to work at 100%. And my bet is that they're not gonna like it, so team-wise, you'll lose all the productivity increase you gain in the first place by using Lisp.
IF you can find a team of lispers that also have webdesign and frontend coding skills, great, but this will be fucking hard as far as I know it. I would just love working in such an environment, but it seems like a non-existent Nirvana right now...
It also compiles to js, so you can even share templates between the client and the backend.
I'm experimenting with using both sexml and closure-template, but in the book I use almost exclusively sexml to make the code simpler. Also s-expressions are much easier to type in than sgml :)
For Clojure - it sounds like you're looking for Christophe Grand's work on enlive: https://github.com/cgrand/enlive
You can read a tutorial here: https://github.com/swannodette/enlive-tutorial/
I'm kidding, but not entirely. Weblocks is an extremely powerful and sophisticated framework, but it has a bit of a learning curve. It desperately needs a good tutorial.
Right now, you're treating it like they're all "just there".
:)