1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ClojureのオブジェクトをClojureScriptに変換する

Last updated at Posted at 2014-12-08

とっさにわからなかったのでメモ
Clojureでpr-strしてClojureScriptでread-stringすればいい

Luminusが標準で使うSelmerを使ってJavaScript経由で渡す場合、pr-strをもう1回使ってJavaScript用のエスケープをしておく。

Selmerのフィルターとしてcljsを作っておくと便利

clj
(ns sample
  (:require [selmer.filters :as f]))

(f/add-filter! :cljs  (fn [x] [:safe (pr-str (pr-str x))]))
html
<script type="text/javascript">
    var value = {{value|cljs}};
</script>
cljs
(ns sample
  (:require [cljs.reader :as r]))

(r/read-string js/value)

※追記
このやり方だと</script>があるとXSS発生してしまうので、display:noneの領域に出力しておいて goog.string.unescapeEntitiesを使ってunescapeするほうがいいかも・・・

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?