Enlive模板 - 添加到头部 [英] Enlive templates – add to head section

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

问题描述

我的应用程序的一些页面将有自己的js / css包含,所以我想知道我如何可以添加这些资源到一个html文档的头部与Enlive。我发现
append变压器,但没有html-append没有自动转义。

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?

推荐答案

其他答案可能早于激活hiccup样式的帮助者。回答已采取并从以下位置展开: Enlive模板 - 将CSS添加到< head>

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])

生成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 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:

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

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

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