HTML Creation in Racket

What is the recommended way to generate HTML from X expressions in Racket? It looks like response/xexpr will do this, but it seems to be designed to serve HTTP responses. The only thing I need is to create an HTML string from the lists of Racket (or X-expressions), I do not need to use a web server.

+6
source share
2 answers

The xexpr-> string function in the xml library should do what you ask if I'm not mistaken. As an example, where it is used, you can look here where the example uses it to generate HTML responses for a simplified web server application.

 > (xexpr->string '(html (head (title "Hello")) (body "Hi!"))) "<html><head><title>Hello</title></head><body>Hi!</body></html>" 
+6
source

If you want to reset xexprs for a potentially more convenient tool, then there is a new language that is used to create Racket web pages. It is not yet documented (therefore it is still new and not publicly available), but you can see how it is used in these sources . As a quick example demonstrating this, run this:

 #lang scribble/html @(define name "foo") @html{@head{@title{@name}} @body{@h1{@name}}} 

Another example uses it as a library:

 #lang at-exp racket/base (require scribble/html) (define (page name) (output-xml @html{@head{@title{@name}} @body{@h1{@name}}})) @page{foo} 

at-exp not required, it just makes it easy to write a lot of text to code. (And that would also be useful with xexprs too.)

The main difference is that in this language HTML tags are actually bindings, which makes it convenient to maintain code. It is also very flexible in that it can be interpreted as text - for example, there is no need to store it in a strict list of lines and subtag, so you never come across issues such as where to use the append-map , etc.

+5
source

Source: https://habr.com/ru/post/908282/


All Articles