[racket] Escaping things in templates?

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sun Oct 23 03:44:13 EDT 2011

As an aside, useful HTML templates don't have to be in HTML syntax; 
Racket programmers can do this:

#lang racket/base

(require (planet neil/html-template:1:0))

(define (write-fastest-in-the-west-page-html thing)
    (html (head (title "Fastest " (% thing) " in the West!")
                (body (h1 "Bang!")
                      (h2 "Bang & Olufson!"))))))

(write-fastest-in-the-west-page-html "Strunk & White")

Which outputs this:

<html><head><title>Fastest Strunk &amp; White in the 
West!</title><body><h1>Bang!</h1><h2>Bang &amp; 

Note the escaping by default of both the static (from the procedure's 
perspective) "&", in "Bang & Olufson", and the dynamic one, in "Strunk & 

For possible improved performance, syntax transformation of that 
procedure expands it to the following, during compilation:

(define (write-fastest-in-the-west-page-html thing)
     (display "<html><head><title>Fastest ")
     (write-html thing)
     (display " in the West!</title><body><h1>Bang!</h1><h2>Bang &amp; 


Posted on the users mailing list.