Enlive模板 - 添加CSS包括< head> [英] Enlive templating - Adding CSS includes to <head>

查看:130
本文介绍了Enlive模板 - 添加CSS包括< head>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我该怎么接近这个。我有一个CSS文件列表,我想要馈入某事,并得到HTML回来。例如,

I'm not sure how I should be approaching this. I have a list of CSS files that I want to feed into something and get HTML back. For example,

(list "base.css" "index.css" "more_css.css") ;vector might be more appropriate?

应转换为:

<link href="css/base.css" rel="stylesheet" />
<link href="css/index.css" rel="stylesheet" />
<link href="css/more_css.css" rel="stylesheet" />

从那里它应该被附加到< head>

From there it should be appended into <head>.

defsnippet 几乎看起来合适,但会为该文件的某个部分带有模板文件和选择器。这里生成的HTML不依赖于模板,只生成HTML的东西似乎是合适的。 clone-for 可能会做我想要的循环部分,但我无法弄清楚如何使用它。

defsnippet almost looks appropriate but takes a template file and a selector for a section of that file. The generated HTML here is not dependent on a template and something that only generates the HTML seems appropriate. clone-for might do the looping part of what I want but I'm having trouble figuring out how to use it.

推荐答案

或者:

(require '[net.cgrand.enlive-html :as enlive])

(defn include-css [href]
      (first (enlive/html [:link {:href href :rel "stylesheet"}])))

(map include-css ["css/base.css" "css/index.css" "css/more_css.css"])
;; newlines added by hand for clarity
=> ({:tag :link, :attrs {:href "css/base.css", :rel "stylesheet"}, :content ()} 
    {:tag :link, :attrs {:href "css/index.css", :rel "stylesheet"}, :content ()} 
    {:tag :link, :attrs {:href "css/more_css.css", :rel "stylesheet"}, :content ()})

仔细检查它是否产生正确的HTML:

Double-check it produces the correct HTML:

(print (apply str (html/emit* (map include-css ["css/base.css" "css/index.css" "css/more_css.css"]))))
;; newlines added by hand for clarity
=> <link href="css/base.css" rel="stylesheet" />
   <link href="css/index.css" rel="stylesheet" />
   <link href="css/more_css.css" rel="stylesheet" />
   nil

这篇关于Enlive模板 - 添加CSS包括&lt; head&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆