Skip to content

Repository files navigation

The irresponsible clojure guild presents...

Thyroid

Clojars ProjectJitpackBuild Status

HTML5 templating made differently insane.

Usage

(require '[irresponsible.thyroid :as t])
(defresolver (t/template-resolver {:type:file:prefix"resources/templates/":suffix".html":cache?false}))
(defengine (t/make-engine {:resolvers [resolver]}))
;; Render data to a string
(t/render engine "hello.html" {"title""Hello World!"})

The template file is defined as

<!-- resources/templates/hello.html --><html><head><titleth:text="${title}">No title set</title></head><body><h1th:text="${title}">fake title</h1></body></html>

Template resolvers

You can define your own template resolvers like this

(import '(org.thymeleaf.templateresolver ITemplateResolver))
(defmethodt/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.

Each dialect expects a handler that returns a set of processors that will be called when the dialect is being rendered.

(import '(org.unbescape.html.HtmlEscape))
(defnsay-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) "!")))))
(defmy-dialect (t/dialect {:name"Hello Dialect":prefix"hello":handler (fn [prefix] #{(say-hello-processor prefix)})}))
;; Render string template
(defengine (t/make-engine {:resolvers [(thyroid/template-resolver {:type:string})]}))
(t/render engine "<p hello:sayto="World">Hi ya!</p>") 

The above example will render <p>Hello, World!</p>

Contributors

Copyright and License

MIT License

About

HTML5 templating made differently insane

Topics

Resources

Stars

5 stars

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages