Skip to content

fhur/gabo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gabo

A minimal templating language for learning purposes only. Read the blog post here

Clojars Project

Usage

First things first:

(use 'gabo.core)

There are only 3 functions you should be using:

(render template context)

Renders a template with the given context. See the template syntax section on how to write templates.

(parse template)

Parses and compiles a template. The result is a tree representation of the template which can later be used by eval-tree

(eval-tree tree context)

Evaluates a tree as produced by parse with the given context. The difference between render and eval-tree is that render compiles a template every single time while eval-tree reuses the compiled template.

Template syntax

Templates are written in syntax very similar to mustache templates.

Example:

(def template
"<p> Hi {{name}} Your friends are: </p>
<ol>
{{#friends '\n'}}<li>{{.}}</li>{{/friends}}
</ol>")

(render template {:name "Bob" :friends ["Billy", "Ann", "Rob", "Sue"]})

;; Output:
"<p> Hi Bob Your friends are: </p>
<ol>
<li>Billy</li>
<li>Ann</li>
<li>Rob</li>
<li>Sue</li>
</ol>"

Syntax:

Symbols {{somevar}}

Symbols are replaced with the textual representation of whatever you supply as context.

Example:

user=> (render "Hi {{name}}" {:name "Charly"})
"Hi Charly"
Iterating {{#coll}} body {{/coll}}

To iterate over a collection you need to specify the name of the collection. You can supply an optional separator argument using the following syntax {{#coll 'foo'}}.

Example:

user=> (render "{{#numbers}}{{.}}{{/numbers}}" {:numbers [1 2 3 4 5]})
"1,2,3,4,5"
user=> (render "{{#numbers '\n'}}{{.}}{{/numbers}}" {:numbers [1 2 3 4 5]})
"1\n2\n3\n4\n5"

As you can see, the default separator is ',' but you can supply your own separator, even no separator at all ('').

You can also supply a map as the elements of the collection being iterated:

user=> (render "{{#friends '\n'}}Name: {{name}}, age {{age}}{{/friends}}"
               {:friends [{:name "Bob" :age 20}
                          {:name "Betty" :age 25}]})
"Name: Bob, age 20\nName: Betty, age 25"

License

Copyright © 2015 Fernando Hurtado

Distributed under the Eclipse Public License, see LICENCE

About

A minimal templating language for learning purposes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published