• Stars
    star
    696
  • Rank 65,018 (Top 2 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created almost 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lisp/Hiccup style templating for Facebook's React in ClojureScript.

ŜABLONO

https://img.shields.io/clojars/v/sablono.svg https://github.com/r0man/sablono/workflows/Clojure%20CI/badge.svg https://versions.deps.co/r0man/sablono/status.svg https://versions.deps.co/r0man/sablono/downloads.svg

Lisp/Hiccup style templating for Facebook’s React in ClojureScript.

http://imgs.xkcd.com/comics/tags.png

Dependencies

Ŝablono doesn’t declare a dependency on React anymore. Use the React dependencies from one of the ClojureScript wrappers or provide the dependencies yourself like this:

[cljsjs/react "16.6.0-0"]
[cljsjs/react-dom "16.6.0-0"]

If you want to do server rendering and use the render or render-static functions from the sablono.server namespace you need to add the following dependency as well:

[cljsjs/react-dom-server "16.6.0-0"]

Usage

Most functions from Hiccup are provided in the sablono.core namespace. The library can be used with Om like this:

(ns example
  (:require [om.core :as om :include-macros true]
            [sablono.core :as html :refer-macros [html]]))

(defn widget [data]
  (om/component
   (html [:div "Hello world!"
          [:ul (for [n (range 1 10)]
                 [:li {:key n} n])]
          (html/submit-button "React!")])))

(om/root widget {} {:target (. js/document (getElementById "my-app"))})

By default, Ŝablono will wrap any forms in the body of hiccup with a call to sablono.interpreter/interpret. If your code returns a React element, then you can skip the call to this function by marking the s-expression with the metadata tag :inline. For example:

[:div {}
 ^:inline (function-that-returns-react-component)]

HTML Tags

Ŝablono only supports tags and attributes that can be handled by React. This means you can’t have your own custom tags and attributes at the moment. For more details take a look at the Tags and Attributes section in the React documentation.

HTML Attributes

HTML attributes in React are camel-cased and the class and for attributes are treated special. Ŝablono renames attributes with dashes in their name to the camel-cased version and handles the class and for special case. This is more consistent with Hiccup and naming conventions used in Clojure.

An input element with event listeners attached to it would look like this in Ŝablono:

(html [:input
       {:auto-complete "off"
        :class "autocomplete"
        :on-change #(on-change %1)
        :on-key-down #(on-key-down %1)
        :type "text"}])

Setting innerHTML of a DOM node

It is not recommended to directly set the innerHTML of DOM nodes, but in some cases it is necessary. i.e. injecting a HTML string that was generated from Markdown.

(html [:div {:dangerouslySetInnerHTML {:__html "<div>hello world</div>" }}])

You can read more at React’s special attributes.

Benchmark

Benchmark results can be found here.

FAQ

How to run the tests?

You need to have Node.js and PhantomJS installed for the ClojureScript test.

Make sure you have all dependencies installed. The following command installs the Maven and Node.js dependencies.

lein deps

To run all Clojure and ClojureScript tests run the following command:

lein ci

For development the ClojureScript tests can be run with lein-doo. To run the tests on Node.js run the following command:

lein doo node nodejs auto

To run the tests on PhantomJS use this command:

lein doo phantom none auto

Why is there a compiler and an interpreter?

The interpreter is executed at run time, and it’s job is to evaluate Hiccup forms and produce React elements. The compiler on the other hand, is executed at compile time and can optimize certain Hiccup forms. It’s job is to evaluate Hiccup forms and produce executable code.

A good introduction to this topic can be found in Peter Seibel’s Practical Common Lisp:

Thanks

This library is based on James Reeves excellent Hiccup library. The server side rendering code has been taken from om.next.

License

Copyright © 2013-2020 r0man

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

More Repositories

1

cljs-http

A ClojureScript HTTP library.
Clojure
576
star
2

sqlingvo

A Clojure & ClojureScript DSL for SQL
Clojure
210
star
3

inflections-clj

Rails-like inflection library for Clojure and ClojureScript
Clojure
207
star
4

ring-cors

Ring middleware for Cross-Origin Resource Sharing.
Clojure
165
star
5

oauth-clj

Clojure OAuth library
Clojure
93
star
6

geocoder-clj

A Clojure library for various geocoder services.
Clojure
43
star
7

soundklaus.el

Play music on SoundCloud with Emacs via EMMS
Emacs Lisp
42
star
8

svm-clj

A LibSVM wrapper for Clojure
Clojure
34
star
9

geo-clj

Experimental Clojure geo library.
Clojure
29
star
10

docopt.el

A Docopt implementation in Emacs Lisp.
Emacs Lisp
27
star
11

hdfs-clj

A Clojure library for the Hadoop Distributed File System
Clojure
27
star
12

.emacs.d

My Emacs configuration
Emacs Lisp
25
star
13

asahi-guix

Asahi Linux on GNU Guix
18
star
14

datumbazo

A JDBC driver for SQLingvo
Clojure
16
star
15

netcdf-clj

NetCDF Clojure Library
Clojure
14
star
16

closure-templates-clj

Clojure Library for Google's Closure Templates.
Clojure
11
star
17

grafeo

A GraphQL document and schema language based on S-expressions in Clojure & ClojureScript
Clojure
11
star
18

validation-clj

A validation library for Clojure.
Clojure
11
star
19

dotfiles

My Dot Files
Shell
10
star
20

commandline-clj

Clojure command line parsing library.
Clojure
9
star
21

noencore

Clojure/ClojureScript functions not in core.
Clojure
9
star
22

acts_as_rateable

Fork of Juixe Software's acts_as_rateable plugin.
Ruby
9
star
23

paimon.el

An Emacs mode for Splunk
Emacs Lisp
9
star
24

geonames-clj

Clojure API for GeoNames.
Clojure
8
star
25

sqlingvo.node

A ClojureScript driver for SQLingvo on Node.js.
Clojure
8
star
26

google-maps-clj

Google Maps Clojure Library
Clojure
8
star
27

postgis.spec

Clojure specs and generators for PostGIS types
Clojure
8
star
28

hive-el

Hive SQL mode extension for Emacs
Emacs Lisp
7
star
29

mahout-in-action

Clojure Examples from Mahout in Action
Clojure
7
star
30

hal-clj

A Clojure library to build HAL maps.
Clojure
6
star
31

acts_as_voteable

Fork of Juixe Software's acts_as_voteable plugin.
Ruby
5
star
32

migrate-clj

Rails-like Database Migration for Clojure
Clojure
5
star
33

rum-mdc

Material Design components for Rum
CSS
5
star
34

closure-lint-mode

An Emacs Mode for the Closure Linter
5
star
35

cascalog-nutch

Cascading Schemes and Cascalog helpers for Nutch sequence files.
Clojure
4
star
36

routes-clj

A Clojure & ClojureScript library to build url and path fns.
Clojure
4
star
37

lein-dpkg

Leiningen plugin for the Debian package management system
Clojure
4
star
38

flickrj

Java
4
star
39

freebase-clj

Clojure Library for Freebase.
Clojure
3
star
40

rest-clj

Clojure
2
star
41

request-clj

A HTTP library for Clojure & ClojureScript.
Clojure
2
star
42

diplomarbeit

Emacs Lisp
2
star
43

lein-env

Leiningen project environments.
Clojure
2
star
44

plz-media-type

A plz.el extension library to handle media types.
Emacs Lisp
2
star
45

conduit-rabbitmq

A library for building distributed applications in Clojure using RabbitMQ
Clojure
1
star
46

ovh-clj

A Clojure Library for the OVH Web Services.
Java
1
star
47

vertica-el

Vertica SQL mode extension for Emacs
Emacs Lisp
1
star
48

immutant-stackoverflow

Clojure
1
star
49

om-unmount-test

Clojure
1
star
50

relajso

Clojure
1
star
51

conduit

A library for stream processing in Clojure
Clojure
1
star
52

cascalog-printtap

A Cascalog tap that prints tuples via Clojure's pr-str function.
Clojure
1
star
53

arrows

A library for creating arrows in Clojure
Clojure
1
star
54

r0man.github.com

My GitHub pages.
1
star
55

guix-system

Scheme
1
star
56

sqlingvo.ksql

A Clojure DSL to build SQL statements for KSQL DB.
Clojure
1
star
57

ring-graphql-multipart

Ring middleware for GraphQL multipart form requests aka file uploads
Clojure
1
star