首页 > 解决方案 > 在enlive中对模板进行多次传递的最佳方法是什么

问题描述

我想获取一个获取的 html 页面并从中创建一个活动模板

(-> (fetch) ; function returns clj-http.client/get
    (:body)
    (clojure.java.io/reader)
    (html/html-resource) ; enlive
    (swap-header)
    (swap-body-tag))

我的两个交换 fns 看起来像这样

(defn swap-header [body]
  (as-> (html/deftemplate temp body [content] [:body] (html/content content)) x
        (apply str (x "testing"))))

(defn swap-body-tag [body]
  (as-> (html/deftemplate temp body [disabled] [:body] (html/set-attr :disabled disabled)) x
        (apply str (x "yo-123"))))

所以困境是我可以从获取返回管道中注释掉这两个交换中的任何一个,它工作正常。尝试(以任何一种顺序)都会失败。我相信应用模板的结果是错误的格式,无法从中制作另一个模板,但无法完全到达我需要的位置。

我链接它们时得到的错误是HTML resource not found.

编辑为使用更多填写的代码并删除 comp

更新我从https://stackoverflow.com/a/38284236/736368 复制了一个函数,以使字符串成为阅读器。这似乎让这个工作。从性能的角度来看,这似乎是一种好方法吗?

(->> ctx
    (:request)
    (fetch)
    (:body)
    (clojure.java.io/reader)
    (html/html-resource)
    (swap-body-tag)
    (apply str)           ; new 
    (string->stream)      ; new
    (swap-header))

标签: clojureenlive

解决方案


您在调用中是否有反转功能comp?它们首先从右侧应用。也许:

((comp swap-urls clean-footer clean-header) parsed)

就像这样:

(-> parsed
  clean-header
  clean-footer
  swap-urls)

我更喜欢线程优先宏comp,因为我认为它更清晰。


推荐阅读