Clojure Enlive模板&x2013;添加到标题部分
我的应用程序的某些页面将包含自己的js/css,因此我想知道如何使用Enlive将这些资源添加到html文档的标题部分。我发现Clojure Enlive模板&x2013;添加到标题部分,clojure,enlive,Clojure,Enlive,我的应用程序的某些页面将包含自己的js/css,因此我想知道如何使用Enlive将这些资源添加到html文档的标题部分。我发现 “append”转换器,但没有自动转义的“html append”。或者说这是一种什么样的正确方法?在Enlive中,您可以在以下位置编写模板: 然后使用enlive模板规则交换内容: (deftemplate microblog-template "net/cgrand/enlive_html/example.html" [title posts] [:ti
“append”转换器,但没有自动转义的“html append”。或者说这是一种什么样的正确方法?在Enlive中,您可以在以下位置编写模板: 然后使用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"}]))
(应用str(微博模板“你好用户!”
[{:标题“职位#1”
:body“你好,危险人物:&”}
{:标题“职位#2”
:body“dolor ipsum”}]))
因此,在第一个HTML模板中,只需编写必要的Java脚本和CSS文件导入。
请注意,您还可以定义可重复使用的子模板以保持干燥。因此,我找到了一种为每个模板添加媒体资源的方法。在我的基本模板中,我有一个非html标记,在页面的模板文件中,有这样一个“更多媒体”部分,包含所需的内容,代码片段将插入到基本模板中(内容),最后我做(展开)。有点棘手,但它可以工作,而且clojure代码中没有html数据。我想我找到了一个解决方案,尽管我远不是Enlive pro(几周前才开始),所以请耐心等待 以下转换假定
链接包含的是一系列
(具有适当的值)
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
从名称空间开始):
我从中借用了一个简单的HTML模板,并将其保存在src/stackoverflow/add\u to\u head\u 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>
启动lein2 repl
后,最后一个print
函数将启动并打印预期结果
我可能一直在使用Enlive的sniptest
,但正如我提到的,我还没有习惯它。其他答案可能早于Enlive打嗝式助手。答案已从以下位置获取并展开:
用于生成HTML节点的函数(简单得多):
用法示例:
;; 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"])))
;;示例模板/base.html文件
(def jquery)http://code.jquery.com/jquery-1.11.0.min.js") ; 链接也起作用
(html/deftemplate主页“templates/base.html”
[]
[:head](html/append(映射包括css[“css/some_file”“css/index.css]”)
[:head](html/append(映射包括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
(打印(应用str(主页)))
;; 为清晰起见,手动添加了换行符
=>
无
是的,我知道这一点,我可以更改title标记的内容,因为我可以使用选择器,但我需要将附加(而不是内容,而是节点)添加到父节点。是否将包含作为HTML文本的blob或URL集合或其他内容?哦,html内容很慢。我只想添加一系列的“”和js包含。我想为每个页面/模板定义这些,并在需要时使用它们。例如,在游戏中!框架我在基础模板中使用“moreScripts”占位符,然后我可以在派生视图中添加任何脚本/样式-(例如:)。
(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)))
(require '[net.cgrand.enlive-html :as 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"])))
(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