weave
1 Example
2 Syntax
3 Weaving and template files
weave
4 Interpolation
9.0.0.11

weave🔗ℹ

ThePuzzlemaker

 #lang weave package: weave

Weave is a dead-simple text templating system for Racket.

1 Example🔗ℹ

In the file "hello.rkt":
#lang weave
@@(define (hello to)
    "Hello, @{to}!")
This is a test of weave!
 
@(hello "world")
@(hello "weave")
@(string-trim (weave "goodbye.rkt" '("world" "weave")))

In the file "goodbye.rkt":
#lang weave (hellos)
@@(define (goodbye to)
    "Goodbye, @{to}!")
@(string-join
  (for/list ([hello (in-list hellos)])
    (goodbye hello))
  "\n")

Running "hello.rkt" prints:

This is a test of weave!

 

Hello, world!

Hello, weave!

Goodbye, world!

Goodbye, weave!

2 Syntax🔗ℹ

Weave’s #lang syntax is quite simple:
  • @(form) is equivalent to (format "~a" form). For example, @(+ 1 2) would expand to "3".

  • @@(form) executes the given code, but does not print any value. Additionally, a single newline after a @@ form is ignored, if one is present.

  • @{value} displays the value inline. For example, @{name}, if name was defined to be "Wren", would simply expand to "Wren"

  • \@ expands to a @ verbatim and prevents any Weave forms from being expanded, e.g. \@(+ 1 1) expands to "@(+ 1 1)".

3 Weaving and template files🔗ℹ

You can create template files by using #lang weave. These files, when executed, will print out their templated result, or you can use the weave macro to get the templated result as a string.

These template files may have arguments specified on the same line as #lang weave in a list, for example:
#lang weave (time day)
It is @{time} on @{day}.
Note that, currently, only identifiers are allowed; no default parameters or variable-argument templates are supported.

Parameters will be #f when a template file with arguments is executed as a main module.

syntax

(weave template-path argument ...)

Returns a string created by providing the given arguments to the file at template-path.

4 Interpolation🔗ℹ

 (require weave/interpolate) package: weave

Weave also has built-in string interpolation, implemented by replacing #%datum if weave/interpolate is required.

Example:
> (require weave/interpolate)
> (println "1 + 1 = @(+ 1 1)")

"1 + 1 = 2"