https://github.com/sunng87/hbs
clojure templating by handlebars.java
https://github.com/sunng87/hbs
Last synced: 5 months ago
JSON representation
clojure templating by handlebars.java
- Host: GitHub
- URL: https://github.com/sunng87/hbs
- Owner: sunng87
- Created: 2013-01-04T14:10:02.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2021-01-09T15:30:31.000Z (about 5 years ago)
- Last Synced: 2024-12-10T07:11:15.465Z (about 1 year ago)
- Language: Clojure
- Size: 111 KB
- Stars: 50
- Watchers: 5
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# hbs
Real-world Clojure templating, seriously. Don't talk about enlive or
hiccup on a clojure web development book any more.
* Never ruin your Clojure code with HTML
* Never ruin your HTML code with Clojure
* Templating without reuse mechanism is shit
* Templating without customization is nothing but shit
What handlebars has?
* Separate your Clojure and HTML, calm down both
* Reuse your handlebars template with **include**, **partial** and **block**
* Create your own **helpers** for your infinite customization needs
This library is based on [handlebars.java](https://github.com/jknack/handlebars.java/).
[](https://travis-ci.org/sunng87/hbs)
## Usage
### Leiningen

### Using hbs.core
```clojure
(require '[hbs.core :as hbs])
;; render template string
(render "Hello {{person.name}}" {:person {:name "World"}})
;; render template file with a registry
(def reg (registry (classpath-loader "/templates" ".tpl")))
(render-file reg "hello" {:name "World"})
```
### Using hbs in Ring application
hbs library provides a default ring middleware
`hbs.core/wrap-handlebars-template` to generate pages from standard
response: `{:hbs {:template ... :context ...}}`
```clojure
;; ring handler function that generate handlebars response
(defn some-handler [req]
;;...
{:hbs {:template "index"
:context {:a "A" :b "B"}}})
```
By default, hbs middleware use `text/html; charset=utf-8` as content
type. You can override it by setting `{:headers {"Content-Type" ...}}`
explicitly in your response.
### Extending hbs.helper
Handlebars is nothing without **helpers**.
```clojure
(use '[hbs core helper])
;; def a helper
(defhelper mytag [ctx options]
(safe-str "HelloWorld, " (clojure.string/upper ctx)))
(def reg (registry (classpath-loader "/templates" ".tpl")))
;; register the helper into registry
(register-helper! registry "mytag" mytag)
(render "{{mytag foo}}" {:foo "bar"})
```
### Using javascript helpers
Helpers can also be defined using javascript. Javascript helpers
are registered using register-js-helpers!-function
```clojure
(register-js-helpers! registry "path/to/file.js")
```
### Helpers defined by me
I have some predefined helpers used in my projects. And I decide to
ship it in the release.
To use these helpers, be sure eval `hbs.ext`. You can
just add a `(:require [hbs.ext])` on you core namespace.
Available helpers:
* ifequals
* ifgreater
* ifless
* ifcontains
* uppercase
* lowercase
* or
* count
* format-date
* format
* ifempty
* max
* min
You can find usage examples of these helpers in the test case `test/hbs/ext_test.clj`.
### Accessing clojure values
By default, `hbs` library provides a `clj-value-resolver` that accepts both keywords and strings
```clojure
(hbs/render "{{ person.name }}" {:person {"name" "World"}})
=> "World"
```
Between strings and keywords, it will prefer strings
```clojure
(hbs/render "{{ person.name }}" {:person {:name "from kw"
"name" "from str"}})
=> "from str"
```
you can also extend and implement your own value resolver
```clojure
(hbs/render "{{ person_name }}"
(-> {:person/name "world"}
Context/newBuilder
(.push (into-array ValueResolver [;; if you want, you use the standard hbs first. And just "complement" it.
hbs/clj-value-resolver
(reify ValueResolver
(resolve [_ context ident]
(get context (some-> ident
(string/replace #"_" "/")
keyword)
ValueResolver/UNRESOLVED)))]))
.build))
=> "world"
```
Checkout [ValueResolver](https://javadoc.io/doc/com.github.jknack/handlebars/latest/com/github/jknack/handlebars/ValueResolver.html)
## See also
Handlebars implemented for Rust language: [handlebars-rust](https://github.com/sunng87/handlebars-rust).
## License
Copyright © 2013-2018 Sun Ning
Distributed under the Eclipse Public License, the same as Clojure.