アプリの一部のページには独自の js/css インクルードが含まれているため、これらのリソースを Enlive を使用して HTML ドキュメントの head セクションに追加するにはどうすればよいでしょうか。「append」トランスフォーマーを見つけましたが、自動エスケープなしの「html-append」はありません。または、それを行う適切な方法は何ですか?
4 に答える
他の答えは、しゃっくりスタイルのヘルパーを活性化する前に発生する可能性があります。Enlive templating - Adding CSS includes to <head>から取得および展開された回答。
(require '[net.cgrand.enlive-html :as html])
HTML ノードを生成するための関数 (はるかに単純):
(defn include-js [src]
(first (html/html [:script {:src src}])))
(defn include-css [href]
(first (html/html [:link {:href href :rel "stylesheet"}])))
使用例:
;; Example templates/base.html file
<html>
<head>
</head>
<body>
</body>
</html>
(def jquery "http://code.jquery.com/jquery-1.11.0.min.js") ; links work as well
(html/deftemplate home-page "templates/base.html"
[]
[:head] (html/append (map include-css ["css/some_file" "css/index.css"]))
[:head] (html/append (map include-js [jquery "js/index.js"])))
正しい HTML が生成されることを確認します。
(print (apply str (home-page)))
;; newlines added by hand for clarity
=> <html>
<head>
<link href="css/some_file" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="js/index.js"></script>
</head>
<body>
</body>
</html>
nil
私は Enlive のプロにはほど遠いですが (数週間前に始めたばかりです)、解決策を見つけたと思いますので、ご容赦ください。
次の変換では、 (適切な値を持つ)link-includes
のシーケンスを想定しています。<link rel="stylesheet" href="/css/this-page-custom.css">
[:head] (html/append
(for [link link-includes]
(-> link
html/html-snippet)))
tl;dr: を使用して、まったく新しいプロジェクトを開始しましたlein2
。
jacek:~/sandbox
$ lein2 new stackoverflow/add-to-head-section
Generating a project called stackoverflow/add-to-head-section based on the 'default' template.
To see other templates (app, lein plugin, etc), try `lein help new`.
そして追加されまし[enlive "1.0.1"]
たproject.clj
(さらに:main
名前空間lein2 repl
は名前空間で始まります):
jacek:~/sandbox/add-to-head-section
$ cat project.clj
(defproject stackoverflow/add-to-head-section "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
[enlive "1.0.1"]]
:main stackoverflow.add-to-head-section.core)
Twitter Bootstrap の Getting Startedから簡単な HTML テンプレートを借りて、 .xmlに保存しましたsrc/stackoverflow/add_to_head_section/index.html
。
jacek:~/sandbox/add-to-head-section
$ cat src/stackoverflow/add_to_head_section/index.html
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
</head>
<body>
<h1>Hello, world!</h1>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</body>
</html>
src/stackoverflow/add_to_head_section/core.clj
最後のパズルは、次のように Enlive スクリプトを記述することでした。
(ns stackoverflow.add-to-head-section.core
(:require [net.cgrand.enlive-html :as html]))
(html/deftemplate index-html "stackoverflow/add_to_head_section/index.html"
[link-includes]
[:head] (html/append
(for [link link-includes]
(-> link
html/html-snippet))))
(defn render-index-html []
(index-html ["<link rel='stylesheet' href='/css/this-page-custom.css'>"
"<link rel='stylesheet' href='/css/another-custom.css'>"]))
(print (apply str (render-index-html)))
起動するとlein2 repl
、最後のprint
関数が開始され、期待される結果が出力されます。
Enlive の を使っていたかもしれませんがsniptest
、前述したように、まだ慣れていません。
そこで、テンプレートごとにメディア リソースを追加する方法を見つけました。私のベーステンプレートには非htmlタグがあり、ページのテンプレートファイルには必要なものを含む「more-media」セクションがあり、スニペットはそれを(コンテンツ)を含むベーステンプレートに挿入し、最後に(開封します)。少しトリッキーですが、動作し、clojure コードに html データはありません。
Enlive では、テンプレートを単純な HTMLで記述できます。
次に、enlive テンプレート ルールを使用してコンテンツを交換します。
(deftemplate microblog-template
"net/cgrand/enlive_html/example.html"
[title posts]
[:title] (content title)
[:h1] (content title)
[:div.no-msg] #(when (empty? posts) %)
[:div.post] #(for [{:keys [title body]} posts]
(at %
[:h2 :a] (content title)
[:p] (content body)))
[[:a (attr? :href)]] (set-attr :title "it's a link"))
最後に、次のように結果を返します。
(apply str (microblog-template "Hello user!"
[{:title "post #1"
:body "hello with dangerous chars: <>&"}
{:title "post #2"
:body "dolor ipsum"}]))
したがって、最初の HTML テンプレートでは、javascript と CSS ファイルに必要なインポートを記述するだけです。DRY のままにするために、再利用可能なサブテンプレートを定義することもできることに注意してください。