我的应用程序的某些页面将有自己的 js/css 包含,所以我想知道如何使用 Enlive 将这些资源添加到 html 文档的头部部分。我发现
“append”变压器,但没有不自动转义的“html-append”。或者什么是正确的方法来做到这一点?
其他答案可能早于活跃的打嗝式助手。答案取自并扩展自:Enlive 模板 - 将 CSS 添加到 https://stackoverflow.com/q/20811216/894091.
(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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)