Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

{{ mustache }} templates for Clojure[Script].

License

NotificationsYou must be signed in to change notification settings

fotoetienne/cljstache

 
 

Repository files navigation

{{mustache }} templates for Clojure[Script].

Compliant with theMustache spec,including lambdas (jvm only)

Forked fromclostache and updated to be compatible with ClojureScript.

Build StatusClojars Project

Usage

To render a template, just pass a template string and a map of data torender:

(require '[cljstache.core:refer [render]])(render"Hello, {{name}}!" {:name"Felix"})

The map of data can have keyword or string keys

On the JVM, you can also render a resource from the classpath like this:

(require '[cljstache.core:refer [render-resource]])(render-resource"templates/hello.mustache" {:name"Michael"})

Both of these functions support an optional third argument, containing partials (see below).

Examples

Variable replacement

Variables are tags enclosed by two curly brackets (mustaches) andwill 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{{& willnot 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 onebeginning with{{/. Their content is only rendered if the data iseither the boolean valuetrue, a value or a non-empty list.

Template:

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

Data:

{:greettrue}

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 onebeginning with{{/. Their content is only rendered if the data iseither the boolean valuefalse or an empty list.

Template:

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

Data:

{:ignorefalse}

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 nestedvariables 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 ofelements.

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!

Using Partials as Includes

You can use partials as "includes" to build up a document from other pieces.For example, when building a web page, you can have header and footer templatefiles that are included in the main page template. This document describes oneway to do that.

In your project directory (let's call it "my-proj"), create a "resources"directory if you don't already have one. Then,

cd path/to/my-proj/resourcesmkdir templatescd templatestouch header.mustache footer.mustache my-page.mustache

Make header.mustache look something like this:

<!doctype html><html><head><title>{{my-title}}</title></head><body><p>header here!</p>

and footer.mustache look something like:

<p>footer here!</p></body></html>

Edit my-page.mustache to contain:

{{> header}}<h1>{{my-title}}</h1><p>Learn about {{stuff}} here.</p>{{> footer}}

Now, in your source code (in a Compojure project, this might bemy-proj/src/my_proj/handler.clj), in thens macro's :requirevector, add

[clostache.parser:refer [render-resource]][clojure.java.io:as io]

then create arender-page helper function:

(defnrender-page"Pass in the template name (a string, sans its .mustachefilename extension), the data for the template (a map), and a list ofpartials (keywords) corresponding to like-named template filenames."  [template data partials]  (render-resource    (str"templates/" template".mustache")    data    (reduce (fn [accum pt];; "pt" is the name (as a keyword) of the partial.              (assoc accum pt (slurp (io/resource (str"templates/"                                                       (name pt)".mustache")))))            {}            partials)))

(thanks to samflores for that idea ☺). You'd then call this function like so:

(render-page"my-page"             {:my-title"My Title":stuff"giraffes"}             [:header:footer]))

Note that the value for :my-title which you pass in makes its waynot only into the my-page.mustache template, but also down intothe included header.mustache.

Set delimiters

You don't have to use mustaches, you can change the delimiters toanything 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 haveLeiningen 2installed.

To run the spec tests, fetch them like this:

git submodule update --init

And run them against all supported Clojure versions:

lein test-all

Requirements

As cljstache uses Clojure's reader conditionals, cljstache is dependent on both Clojure 1.7 and Leiningen 2.5.2 or later.Java 8 or greater is required to run the clojurescript tests (using Nashorn.)

License

Copyright (C) 2014 Felix H. Dahlke

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

This library is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser General Public License for more details.

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

Contributors

About

{{ mustache }} templates for Clojure[Script].

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Clojure100.0%

[8]ページ先頭

©2009-2025 Movatter.jp