Enlive templates – add to head section
Question
Some pages of my app will have it's own js/css includes, so I wonder how I can add these resources to the head section of an html document with Enlive. I found "append" transformer, but there is no "html-append" without auto escaping. Or what a proper way to do that?
Solution
The other answers might predate enlive hiccup-style helpers. Answer taken and expanded from: Enlive templating - Adding CSS includes to <head>.
(require '[net.cgrand.enlive-html :as html])
Functions for generating HTML nodes (much much MUCH simpler):
(defn include-js [src]
(first (html/html [:script {:src src}])))
(defn include-css [href]
(first (html/html [:link {:href href :rel "stylesheet"}])))
Example usage:
;; 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"])))
Check it produces the correct 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
OTHER TIPS
In Enlive, you can write templates in simple HTML:
Then just swap in the content with enlive template rules:
(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"))
Finally, just return the result with:
(apply str (microblog-template "Hello user!"
[{:title "post #1"
:body "hello with dangerous chars: <>&"}
{:title "post #2"
:body "dolor ipsum"}]))
So, in the first HTML template, just write the necessary imports for javascripts and CSS files. Note that you can also define re-usable sub-templates to stay DRY.
So, I've found a way to add media resources per template. In my base template I have non-html tag , and I in a template file for the page there is such "more-media" section with needed stuff and snippet will insert it into a base template with (content) and finally I do (unwrap). A little bit tricky, but it works and there is no html data in the clojure code.
I think I found a solution although I'm far from being an Enlive pro (just started a couple of weeks ago) so please bear with me.
The following transformation assumes link-includes
is a sequence of <link rel="stylesheet" href="/css/this-page-custom.css">
(with appropriate values).
[:head] (html/append
(for [link link-includes]
(-> link
html/html-snippet)))
tl;dr: I started with a brand new project using 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`.
And added [enlive "1.0.1"]
to project.clj
(plus :main
namespace so lein2 repl
starts in the namespace):
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)
I borrowed a simple HTML template from Twitter Bootstrap's Getting Started and saved it in 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>
The last puzzle was to write an Enlive script in src/stackoverflow/add_to_head_section/core.clj
as follows:
(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)))
Once lein2 repl
is up, the last print
function kicks in and prints the expected outcome.
I might've been using Enlive's sniptest
, but as I mentioned I'm yet to get used to it.