在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:fhd/clostache开源软件地址:https://github.com/fhd/clostache开源编程语言:Clojure 99.9%开源软件介绍: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. UsageThe 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:
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). ExamplesVariable replacementVariables are tags enclosed by two curly brackets (mustaches) and will be replaced with the respective data. Template: Hello, {{person}}! Data: {:person "World"} Output:
Escaped outputThe following characters will be replaced with HTML entities:
Template: Escaped: {{html}}
Unescaped: {{{html}}}
Unescaped: {{&html}} Data: {:html "<h1>Hello, World!</h1>"} Output: Escaped: <h1>Hello, World!</h1>
Unescaped: <h1>Hello, World!</h1>
Unescaped: <h1>Hello, World!</h1> SectionsSections start with a tag beginning with Template: {{#greet}}Hello, World!{{/greet}} Data: {:greet true} Output:
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:
Inverted sectionsInverted sections start with a tag beginning with Template: {{^ignore}}Hello, World!{{/ignore}} Data: {:ignore false} Output:
CommentsComments are tags that begin with Template: <h2>Felix' section<h2>
{{! Look ma, I've written a section }} Output: <h2>Felix' section</h2> Dotted namesDotted names are a shorter and more convenient way of accessing nested variables or sections. Template: {{greeting.text}} Data: {:greeting {:text "Hello, World"}} Output:
Implicit iteratorsImplicit 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> PartialsPartials 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:
Set delimitersYou don't have to use mustaches, you can change the delimiters to anything you like. Template: {{=<% %>=}}
Hello, <%name%>! Data: {:name "Felix"} Output:
LambdasYou can also call functions from templates. Template: {{hello}}
{{#greet}}Felix{{/greet}} Data: {:hello "Hello, World!"}
{:greet #(str "Hello, " %)} Output:
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:
DevelopmentMake sure you have Leiningen 2 installed. To run the spec tests, fetch them like this:
And run them against all supported Clojure versions:
LicenseCopyright (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
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论