https://github.com/jjl/thyroid
HTML5 templating made differently insane
https://github.com/jjl/thyroid
clojure html5 templating thymeleaf thyroid
Last synced: about 2 months ago
JSON representation
HTML5 templating made differently insane
- Host: GitHub
- URL: https://github.com/jjl/thyroid
- Owner: jjl
- License: mit
- Created: 2017-07-07T10:22:44.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-18T18:12:35.000Z (over 8 years ago)
- Last Synced: 2025-10-21T15:33:30.589Z (3 months ago)
- Topics: clojure, html5, templating, thymeleaf, thyroid
- Language: Clojure
- Homepage:
- Size: 33.2 KB
- Stars: 5
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
README
The irresponsible clojure guild presents...
# Thyroid
[](https://clojars.org/irresponsible/thyroid)
[](https://jitpack.io/#irresponsible/thyroid)
[](https://travis-ci.org/irresponsible/thyroid)
HTML5 templating made differently insane.
## Usage
```clojure
(require '[irresponsible.thyroid :as t])
(def resolver (t/template-resolver
{:type :file
:prefix "resources/templates/"
:suffix ".html"
:cache? false}))
(def engine (t/make-engine {:resolvers [resolver]}))
;; Render data to a string
(t/render engine "hello.html" {"title" "Hello World!"})
```
The template file is defined as
```html
No title set
fake title
```
### Template resolvers
You can define your own template resolvers like this
```clojure
(import '(org.thymeleaf.templateresolver ITemplateResolver))
(defmethod t/template-resolver ::custom
[options]
(reify ITemplateResolver
(getName [this] (:name options))
(getOrder [this] (:order options))
(resolveTemplate [this conf owner-template template resolution-attrs]
...)))
```
### Dialects
Adding dialects is a more involved process, this example is a copy of that
presented by the thymeleaf ["Say Hello! Extending Thymeleaf in 5 minutes" guide][1].
Each dialect expects a handler that returns a set of processors that will be
called when the dialect is being rendered.
[1]: http://www.thymeleaf.org/doc/articles/sayhelloextendingthymeleaf5minutes.html
```clojure
(import '(org.unbescape.html.HtmlEscape))
(defn say-hello-processor
[prefix]
(t/process-by-attrs
{:prefix prefix
:attr-name "sayto"
:handler (fn [ctx tag attr-name attr-val struct-handler]
(.setBody struct-handler
(str "Hello, " (HtmlEscape/escapeHtml attr-val) "!")))))
(def my-dialect
(t/dialect
{:name "Hello Dialect"
:prefix "hello"
:handler (fn [prefix]
#{(say-hello-processor prefix)})}))
;; Render string template
(def engine (t/make-engine
{:resolvers [(thyroid/template-resolver {:type :string})]}))
(t/render engine "
Hi ya!
")
```
The above example will render `
Hello, World!
`
## Contributors
* [James Laver](https://github.com/jjl)
* [Antonis Kalou](https://github.com/kalouantonis)
* [Kent Fredric](https://github.com/kentfredric)
## Copyright and License
[MIT License](LICENSE)