0

我在 enlive 中有一些转变:

(html/deftemplate tranforma-numero-template "public/index.html" [m]
              [:#portugues :.conteudo] (html/content (:portugues m))
              [:#ingles :.conteudo] (html/content (:ingles m))
              [:#espanhol :.conteudo] (html/content (:espanhol m)))

它遵循一个模式。所以我想以某种方式抽象出这种模式。我想如果这样的事情是可能的:

(html/deftemplate tranforma-numero-template "public/index.html" [m]
    [(html/pred (fn [node]
        (when (seq formatos)
           (-> node :attrs :id (set formatos)))))] (html/content ((keyword (-> node :attrs :id)) m))) 

我知道对于这样一个简单的例子来说这有点过分了,但你明白了这个概念。

4

1 回答 1

0

正如您在上面所做的那样,编写通用选择器的一种替代方法是动态生成对 deftemplate 的调用。在您的示例中编写智能选择器和转换器也是一种非常好的方法。

因为 deftemplate 是一个宏,如果您的模板创建代码也是一个宏,它可能会更容易。这种宏传染是使宏成为库的主要接口的一个缺点。不幸的是,这意味着这将更加困难。幸运的是,产生对 deftemplate 的调用的宏甚至比智能选择器方法更简单。只需使用适当的选择器和转换序列生成对 deftemplate 的调用即可。

于 2014-04-09T20:47:12.713 回答