1 Installation
1.1 Installing pregexp
1.2 Installing JSON Template for R6RS
2 Usage
2.1 API
2.2 Examples
Version: 5.1.1

JSON Template for R6RS

Matthias A. Benkard

1 Installation

1.1 Installing pregexp

Download the pregexp library from http://evalwhen.com/pregexp/. Wrap "pregexp.scm" in the following form (replacing “....” with the file’s original content):

(library (pregexp)
  (export pregexp
          pregexp-match
          pregexp-match-positions
          pregexp-split
          pregexp-replace
          pregexp-replace*
          pregexp-quote)
  (import (rnrs) (rnrs mutable-pairs))
 
  ....)

You can then install "pregexp.scm" as an R6RS library. For details on how to do this, consult the manual of your Scheme implementation.

As an example,
plt-r6rs --install pregexp.scm
will work on Racket.

1.2 Installing JSON Template for R6RS

JSON Template for R6RS is provided as a ready-to-use R6RS library file. Simply install "json-template.s6l" as per the manual of the Scheme implementation of your choice.

On Racket,
plt-r6rs --install json-template.s6l
ought to work just fine.

2 Usage

2.1 API

(make-template template-data)  procedure?
  template-data : string?
Create a template from template-data, which must be in JSON Template syntax.

The returned procedure expects a single argument, the subtitution context, and returns the expanded template as a string. Three types of contexts are supported:

make-template’s behavior can be customized by the parameters formatters, meta-left, meta-right, default-formatter, and format-char.

For general information about JSON Template, see http://json-template.googlecode.com/svn/trunk/doc/Introducing-JSON-Template.html and http://code.google.com/p/json-template/wiki/Reference.

2.2 Examples

> (define template (make-template "\r\n<h1>{title|html}</h1>\r\n{.section people}\r\n<ul>\r\n{.repeated section @}\r\n  <li>{name} ({age} years)</li>\r\n{.end}\r\n</ul>\r\n{.or}\r\n<p>No one's registered.</p>\r\n{.end}\r\n"))
> (template '((title . "<Registered People>")
              (people
                       ((name . "Nathalie") (age . 24))
                       ((name . "Heinrich") (age . 28))
                       ((name . "Hans")     (age . 25)))))

<h1>&#60;Registered People&#62;</h1>

<ul>

  <li>Nathalie (24 years)</li>

  <li>Heinrich (28 years)</li>

  <li>Hans (25 years)</li>

</ul>

> (template '((title . "<Registered People>")
              (people)))

<h1>&#60;Registered People&#62;</h1>

<p>No one's registered.</p>

> (template '((title . "<Registered People>")))

<h1>&#60;Registered People&#62;</h1>

<p>No one's registered.</p>