Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

Clostache

{{ mustache }} for Clojure.

Compliant with the Mustache spec since version 1.0. Supporting lambdas since version 1.1.

Works with Clojure 1.5 since version 1.5. If you want to use Clostache in Clojure 1.3 or 1.4 projects, use version 1.4. In Clojure 1.2 projects, use version 1.1.

Build Status

Usage

The easiest way to use Clostache in your project is via Clojars.

Leiningen:

[de.ubercode.clostache/clostache "1.4.0"]

Maven:

<dependency>
  <groupId>de.ubercode.clostache</groupId>
  <artifactId>clostache</artifactId>
  <version>1.4.0</version>
</dependency>

To install it via cljr, run:

cljr install de.ubercode.clostache/clostache

This is how you use Clostache:

(use 'clostache.parser)
(render "Hello, {{name}}!" {:name "Felix"})

You can render a resource from the classpath like this:

(use 'clostache.parser)
(render-resource "templates/hello.mustache" {:name "Michael"})

Each function supports an optional third argument, containing partials (see below).

Examples

Variable replacement

Variables are tags enclosed by two curly brackets (mustaches) and will be replaced with the respective data.

Template:

Hello, {{person}}!

Data:

{:person "World"}

Output:

Hello, World!

Escaped output

The following characters will be replaced with HTML entities: &"<>. Tags that use three curly brackets or start with {{& will not be escaped.

Template:

Escaped: {{html}}
Unescaped: {{{html}}}
Unescaped: {{&html}}

Data:

{:html "<h1>Hello, World!</h1>"}

Output:

Escaped: &lt;h1&gt;Hello, World!&lt;/h1&gt;
Unescaped: <h1>Hello, World!</h1>
Unescaped: <h1>Hello, World!</h1>

Sections

Sections start with a tag beginning with {{# and end with one beginning with {{/. Their content is only rendered if the data is either the boolean value true, a value or a non-empty list.

Template:

{{#greet}}Hello, World!{{/greet}}

Data:

{:greet true}

Output:

Hello, World!

In case of a list, the section's content is rendered for each element, and it can contain tags refering to the elements.

Template:

<ul>
{{#people}}
    <li>{{name}}</li>
{{/people}}
</ul>

Data:

{:people [{:name "Felix"} {:name "Jenny"}]}

Output:

<ul>
    <li>Felix</li>
    <li>Jenny</li>
</ul>

For single values, the section is rendered exactly once.

Template:

{{#greeting}}{{text}}!{{/greeting}}

Data:

{:greeting {:text "Hello, World"}}

Output:

Hello, World!

Inverted sections

Inverted sections start with a tag beginning with {{^ and end with one beginning with {{/. Their content is only rendered if the data is either the boolean value false or an empty list.

Template:

{{^ignore}}Hello, World!{{/ignore}}

Data:

{:ignore false}

Output:

Hello, World!

Comments

Comments are tags that begin with {{!. They will not be rendered.

Template:

<h2>Felix' section<h2>
{{! Look ma, I've written a section }}

Output:

<h2>Felix' section</h2>

Dotted names

Dotted names are a shorter and more convenient way of accessing nested variables or sections.

Template:

{{greeting.text}}

Data:

{:greeting {:text "Hello, World"}}

Output:

Hello, World

Implicit iterators

Implicit iterators allow you to iterate over a one dimensional list of elements.

Template:

<ul>
{{#names}}
    <li>{{.}}</li>
{{/names}}
</ul>

Data:

{:names ["Felix" "Jenny"]}

Output:

<ul>
    <li>Felix</li>
    <li>Jenny</li>
</ul>

Partials

Partials allow you to include other templates (e.g. from separate files).

Template:

Hello{{>names}}!

Data:

{:people [{:name "Felix"} {:name "Jenny"}]}

Partials:

{:names "{{#people}}, {{name}}{{/people}}"}

Output:

Hello, Felix, Jenny!

Set delimiters

You don't have to use mustaches, you can change the delimiters to anything you like.

Template:

{{=<% %>=}}
Hello, <%name%>!

Data:

{:name "Felix"}

Output:

Hello, Felix!

Lambdas

You can also call functions from templates.

Template:

{{hello}}
{{#greet}}Felix{{/greet}}

Data:

{:hello "Hello, World!"}
{:greet #(str "Hello, " %)}

Output:

Hello, World!
Hello, Felix!

Functions can also render the text given to them if they need to do something more complicated.

Template:

"{{#people}}Hi {{#upper}}{{name}}{{/upper}}{{/people}}"

Data:

{:people [{:name "Felix"}] 
 :upper (fn [text] 
          (fn [render-fn] 
            (clojure.string/upper-case (render-fn text))))}

Output:

Hello FELIX

Development

Make sure you have Leiningen 2 installed.

To run the spec tests, fetch them like this:

git submodule update --init

And run them against all supported Clojure versions:

lein all test

License

Copyright (C) 2014 Felix H. Dahlke

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

Contributors

Clojure
Clojure
Clojure is a modern, functional programming language that runs on the JVM. It emphasizes immutability and concurrency, making it suitable for scalable applications. Its simplicity and powerful abstractions attract developers in data science and backend development.
Biff | Clojure web framework
Biff | Clojure web framework
Kamalavelan / column · GitLab
Kamalavelan / column · GitLab
Clojure Tutorials
Clojure Tutorials
reborg
reborg
Misophistful
Misophistful
ClojureVids
ClojureVids
GitHub - venantius/photon: Experiments in realtime web framework design. Like Meteor, but for Clojure(Script)
GitHub - venantius/photon: Experiments in realtime web framework design. Like Meteor, but for Clojure(Script)
GitHub - fhd/clostache: {{ mustache }} for Clojure
GitHub - fhd/clostache: {{ mustache }} for Clojure
GitHub - dakrone/cheshire: Clojure JSON and JSON SMILE (binary json format) encoding/decoding
GitHub - dakrone/cheshire: Clojure JSON and JSON SMILE (binary json format) encoding/decoding
GitHub - mrdimosthenis/clj-synapses: A neural networks library for Clojure
GitHub - mrdimosthenis/clj-synapses: A neural networks library for Clojure
GitHub - plumatic/schema: Clojure(Script) library for declarative data description and validation
GitHub - plumatic/schema: Clojure(Script) library for declarative data description and validation
GitHub - http-kit/http-kit: Simple, high-performance event-driven HTTP client+server for Clojure
GitHub - http-kit/http-kit: Simple, high-performance event-driven HTTP client+server for Clojure
GitHub - pedestal/pedestal: The Pedestal Server-side Libraries
GitHub - pedestal/pedestal: The Pedestal Server-side Libraries
GitHub - ztellman/proteus: local. mutable. variables.
GitHub - ztellman/proteus: local. mutable. variables.
GitHub - fulcrologic/fulcro: A library for development of single-page full-stack web applications in clj/cljs
GitHub - fulcrologic/fulcro: A library for development of single-page full-stack web applications in clj/cljs
GitHub - tpope/vim-fireplace: fireplace.vim: Clojure REPL support
GitHub - tpope/vim-fireplace: fireplace.vim: Clojure REPL support
Home
Home
GitHub - clojure-cookbook/clojure-cookbook: This is the home of O'Reilly's Clojure Cookbook - http://clojure-cookbook.com
GitHub - clojure-cookbook/clojure-cookbook: This is the home of O'Reilly's Clojure Cookbook - http://clojure-cookbook.com
GitHub - ertugrulcetin/re-frame-flow: Graph based visualization tool for re-frame event chains
GitHub - ertugrulcetin/re-frame-flow: Graph based visualization tool for re-frame event chains
Build software better, together
Build software better, together
Daniel Amber
Daniel Amber
Build software better, together
Build software better, together
GitHub - technomancy/leiningen: Moved to Codeberg; this is a convenience mirror
GitHub - technomancy/leiningen: Moved to Codeberg; this is a convenience mirror
Build software better, together
Build software better, together
Build software better, together
Build software better, together
GitHub - darkleaf/di: DI is a dependency injection framework that allows you to define dependencies as cheaply as defining function arguments.
GitHub - darkleaf/di: DI is a dependency injection framework that allows you to define dependencies as cheaply as defining function arguments.
Build software better, together
Build software better, together
GitHub - junegunn/rainbow_parentheses.vim: :rainbow: Simpler Rainbow Parentheses
GitHub - junegunn/rainbow_parentheses.vim: :rainbow: Simpler Rainbow Parentheses
GitHub - clojure/core.match: An optimized pattern matching library for Clojure
GitHub - clojure/core.match: An optimized pattern matching library for Clojure
GitHub - yogthos/clojure-error-message-catalog: a catalog of common Clojure errors and their meaning
GitHub - yogthos/clojure-error-message-catalog: a catalog of common Clojure errors and their meaning
Clojure
More on Clojure

  1. Programming Tips & Tricks
  2. Error Solutions
  3. Shortcuts
  4. Collections

Tools

available to use.

Made with ❤️

to provide resources in various ares.