After bread has been secured, leisure is the supreme aim.
— Pyotr Kropotkin
WORK IN PROGRESS. Code is approaching "stable alpha" status. A lot of this still does not work yet.
Bread aims to be three things:
Bread's (planned) high-level feature set:
(ns my-project
(:require
[reitit.core :as reitit]
;; include support for plugging in a Reitit Router
[systems.bread.alpha.core :as bread]
[systems.bread.alpha.component :refer [defc]]
[systems.bread.alpha.plugin.reitit]
[systems.bread.alpha.plugin.defaults :as defaults]))
(defc hello [{:keys [i18n params]}]
[:p (format "%s, %s!" (:greeting i18n) (:to params))])
(def router
(reitit/router
["/:lang" ; :lang is the default i18n route param but can be configured
["/hello/:to" {:name :hello
:bread/dispatcher :component
:data [:params]
:component hello}]]))
(def data
[{:string/key :hello :string/lang :en :string/value "Hello"}
{:string/key :hello :string/lang :fr :string/value "Bonjour"}])
(def handler
(bread/load-handler (defaults/app {:router router
:db {:db/type :datahike
:store {; datahike config
:backend :mem
:id "hello-bread"}
:initial-txns data}})))
(handler {:uri "/en/hello/Breadsters"})
;; => [:p "Hello, Breadsters!"]
(handler {:uri "/fr/hello/Breadsters"})
;; => [:p "Bonjour, Breadsters!"]
(ns my.simple.blog
(:require
[reitit.core :as reitit]
[systems.bread.alpha.core :as bread]
[systems.bread.alpha.component :refer [defc]]
[systems.bread.alpha.plugin.reitit]
[systems.bread.alpha.plugin.defaults :as defaults]))
(defc main-layout [{:keys [main-content i18n lang]}]
{:content-path [:main-content]} ; how extending components pass their content
[:html {:lang lang}
[:body
[:h1 (:site-header i18n)]
main-content]])
(defc article-component
[{:keys [i18n lang]
{:post/keys [slug fields]} :article}] ; :article corresponds to :key
{:query [:post/slug fields*]
:key :article
:extends main-layout}
[:main.single
[:article
[:h2 (:title fields)]
[:div.permalink [:a {:href (str "/" lang "/article/" slug)}]]
(:content fields)]])
(defc article-listing-component
[{:keys [i18n lang articles]}] ; :articles corresponds to :key
{:query [:post/slug fields*]
:key :articles
:extends main-layout}
[:main.listing
(map (fn [{:post/keys [slug fields]}]
[:article
[:a {:href (str "/" lang "/article/" slug)}
(:title fields)]])
articles)])
(def router
(reitit/router
["/:lang"
["/"
{:dispatcher/type :dispatcher.type/articles
:dispatcher/component article-listing-component}]
["/article/:slug"
{:dispatcher/type :dispatcher.type/article
:dispatcher/component article-component}]]))
(def data
[{:string/key :site-header
:string/lang :en
:string/value "My Blog"}
{:post/type :post.type/article
:post/status :post.type/published
:translatable/fields [{:field/key :title
:field/lang :en
:field/format :edn
:field/content "\"English Article Title\""}
{:field/key :title
:field/lang :en
:field/format :edn
:field/content "\"Article content in English...\""}]}
{:post/type :post.type/article
:post/status :post.type/published
:translatable/fields [{:field/key :title
:field/lang :en
:field/format :edn
:field/content "\"An Older Article\""}
{:field/key :title
:field/lang :en
:field/format :edn
:field/content "\"Lorem ipsum dolor sit amet\""}]}])
(def handler
(bread/load-handler (defaults/app {:router router
:db {:db/type :datahike
:store {; datahike config
:backend :mem
:id "bread-blog-db"}}})))
(handler {:uri "/en/"})
;; => [:html {:lang "en"}
;; [:body
;; [:h1 "My Blog"]
;; [:main.listing
;; [:article [:a {:href "/en/newer-article"} "English Article Title"]]
;; [:article [:a {:href "/en/older-article"} "An Older Article"]]]]]
(handler {:uri "/en/article/newer-article"})
;; => [:html {:lang "en"}
;; [:body
;; [:h1 "My Blog"]
;; [:main.single
;; [:article
;; [:h2 "English Article Title"]
;; [:div.content "Article content in English..."]]]]]
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close