clojure - 在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))
解决方案
您在调用中是否有反转功能comp
?它们首先从右侧应用。也许:
((comp swap-urls clean-footer clean-header) parsed)
就像这样:
(-> parsed
clean-header
clean-footer
swap-urls)
我更喜欢线程优先宏comp
,因为我认为它更清晰。
推荐阅读
- python - Pandas:为准备合并,我怎样才能让两个大(130 万行)列的日期/年份格式匹配?
- java - 如何在java中将文本转换为列
- angular - 为什么在我的集成测试中无法获得对 Angular Material 的 mat-chip 的引用?
- java - 使用 Rest Template 对外部网站进行 PUT 调用
- r - 闪亮的情节不会随着输入而改变
- java - 如何使用 NiFi 中的新 DBCPConnectionPoolLookup 列出一组数据库中的数据库表?
- php - 未找到使用 laravel 5.4 发送电子邮件的视图
- javascript - 最大化这个while循环的效率
- javascript - 在 JS 中根据背景颜色切换元素颜色
- django - UpdateView 模板只显示一个相关模型的pk,如何让它显示其他字段?