ANN: cl-json-template

Discussion of Common Lisp
Post Reply
Kompottkin
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany
Contact:

ANN: cl-json-template

Post by Kompottkin » Fri Mar 18, 2011 5:09 am

Summary

I've written an implementation of (most of) JSON Template in the course of rewriting my website.

JSON Template is a minimalistic, declarative template language.

Downloading

You can fetch the Mercurial repository using the following command:

Code: Select all

hg clone http://matthias.benkard.de/code/cl-json-template/
(Mind the trailing slash. My web server is overly picky there.)

Implementation Features
  • No dependencies
  • Portable Common Lisp (tested on SBCL, Clozure CL, ECL, XCL, ABCL)
  • HTML and URI escaping through the use of formatters
  • Apache license
Missing Things
  • Literals (like {.space} and {.meta-left}/{.meta-right})
  • Formatters with arguments
  • Options (like changing the meta character or the default formatter)
  • Some kind of compilation for efficiency
Examples

Code: Select all

JSON-TEMPLATE> (defparameter *tmpl* (parse-template-string "
<h1>{title|html}</h1>
{.section people}
<ul>
{.repeated section @}
  <li>{name} ({age} years)</li>
{.end}
</ul>
{.or}
<p>No one's registered.</p>
{.end}"))
*TMPL*

Code: Select all

JSON-TEMPLATE> (expand-template *tmpl*
                                '(:title "<Registered People>"
                                  :people ((:name "Nathalie" :age 24)
                                           (:name "Heinrich" :age 28)
                                           (:name "Hans"     :age 25))))
"
<h1><Registered People></h1>
<ul>
  <li>Nathalie (24 years)</li>
  <li>Heinrich (28 years)</li>
  <li>Hans (25 years)</li>
</ul>
"

Code: Select all

JSON-TEMPLATE> (expand-template *tmpl*
                                '(:title "<Registered People>"
                                  :people ()))
"
<h1><Registered People></h1>
<p>No one's registered.</p>
"
Have fun! :)

Post Reply