kumarshantanu.basil

https://github.com/kumarshantanu/basil.git

git clone 'https://github.com/kumarshantanu/basil.git'

(ql:quickload :kumarshantanu.basil)
10

basil

Basil is a general purpose template library for Clojure and ClojureScript.

Important: Consider this library in ALPHA until this notice is removed. API and implementation will change in incompatible ways. You have been warned.

Usage

On Clojars: https://clojars.org/basil

Leiningen dependency: [basil "0.4.0"]

Supported Clojure versions: 1.2, 1.3, 1.4, 1.5

Tested with lein-cljsbuild version: 0.2.8

This page has only elementary documentation. Intro page has more details.

Basic Templates

A Basil template can be parsed from a plain string. Every Basil template is composed of distinct, interleaved and non-nested static-text and slots. The slots contain limited Clojure forms to express the filter-functions and dynamic data. Few examples:

Template Clojure data Result
foo bar {} foo bar
foo <% num %> bar {:num 45} foo 45 bar
foo <% (inc num) %> bar {:inc inc :num 45} foo 46 bar
foo <% (inc (inc num)) %> bar {:inc inc :num 45} foo 47 bar
foo
<% (when-not (seq names) "No names!") %>
bar
{:names []} foo
No names!
bar
foo
<% (when (seq names) (str-br names)) %>
bar
{:names ["Lucy" "Brian"]} foo
Lucy<br/>
Brian<br/>
bar
foo
<% (for-each [b names] (str "name=" b)) %>
bar
{:names ["Tom" "Jane" "Larry"]} foo
name=Tom
name=Jane
name=Larry
bar
foo
<% (for-each [a [1 2]
              b names]
     (str ":a=" a
          ", name=" b)) %>
bar
{:names ["Tom" "Jane" "Larry"]} foo
:a=1, name=Tom
:a=1, name=Jane
:a=1, name=Larry
:a=2, name=Tom
:a=2, name=Jane
:a=2, name=Larry
bar

Use basil.public/parse-compile to parse and compile a template and use basil.public/render-template to render a compiled template.

Template Groups

Template groups can be created from directory or classpath. A slot in one template can include an entire template dynamically using <% (include "foo.basil") %>. You may also create additional context for included template using <% (include "foo.basil" {:a 10 :b 20} {:items xs}) %>.

Use basil.public/make-group-from-directory and basil.public/make-group-from-classpath to create template groups. Use basil.public/render-by-name to render a template from a group.

Getting in touch

By e-mail: kumar.shantanu(at)gmail.com

On Twitter: @kumarshantanu

License

Copyright © 2012 Shantanu Kumar

Distributed under the Eclipse Public License, the same as Clojure.