[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-template
    (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; 
Olufson!</h2></body></head></html>


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

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)
   (begin
     (display "<html><head><title>Fastest ")
     (write-html thing)
     (display " in the West!</title><body><h1>Bang!</h1><h2>Bang &amp; 
Olufson!</h2></body></head></html>")
     (void)))


-- 
http://www.neilvandyke.org/


Posted on the users mailing list.