Skip to content

Commit

Permalink
added compile example on top
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Feb 20, 2024
1 parent 3f48ca1 commit acfb275
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Currently for my personal use. Future breaking changes possible.

# Example

### Runtime HTML Serialization

```clojure
(require '[dev.onionpancakes.chassis.core :as c])

Expand Down Expand Up @@ -52,6 +54,38 @@ Currently for my personal use. Future breaking changes possible.
;; "<!DOCTYPE html><html><head><link href=\"/css/styles.css\" rel=\"stylesheet\"><title>My Blog</title></head><body><h1>My Blog</h1><div id=\"1\"><h2 class=\"title\">foo</h2><p class=\"content\">bar</p></div></body></html>"
```

### Compiled HTML Serialization

```clojure
(require '[dev.onionpancakes.chassis.core :as c])
(require '[dev.onionpancakes.chassis.compiler :as cc])

(defn compiled-my-post
[post]
(cc/compile
[:div {:id (:id post)}
[:h2.title (:title post)]
[:p.content (:content post)]]))

(defn compiled-my-blog
[data]
(cc/compile
[c/doctype-html5 ; Raw string for <!DOCTYPE html>
[:html
[:head
[:link {:href "/css/styles.css" :rel "stylesheet"}]
[:title "My Blog"]]
[:body
[:h1 "My Blog"]
(for [p (:posts data)]
(compiled-my-post p))]]]))

(let [data {:posts [{:id "1" :title "foo" :content "bar"}]}]
(c/html (compiled-my-blog data)))

;; "<!DOCTYPE html><html><head><link href=\"/css/styles.css\" rel=\"stylesheet\"><title>My Blog</title></head><body><h1>My Blog</h1><div id=\"1\"><h2 class=\"title\">foo</h2><p class=\"content\">bar</p></div></body></html>"
```

# Usage

Require the namespace.
Expand Down Expand Up @@ -593,15 +627,14 @@ Use `cc/compile*` to ensure the return value is a vector. Otherwise, it is the s
;; [#object[dev.onionpancakes.chassis.core.RawString 0x24f1caeb "<hr>"]]
```



## Ambiguous Attributes Produce Speed Bumps

Ambiguous objects in the second position forces the compiler to emit checks which examine the potential attributes map at runtime.

```clojure
(let [data {:body "foo"}]
(pprint (macroexpand-1
;; Compiler can't see what (:body data) returns.
'(cc/compile [:div (:body data)]))))

;; Results in:
Expand Down

0 comments on commit acfb275

Please sign in to comment.