追加到Enlive中的属性 [英] Append to an attribute in Enlive

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

问题描述

是否可以使用enlive将值附加到属性?



示例:我有这个

 < a href =/ item / edit /> edit< / a> 

,并希望这样

 < a href =/ item / edit / 123> edit< / a> 

我目前正在这样做:

 (html / defsnippet fooviews / foo.html[:#main] 
[ctxt]
[:a](html / set-attr:href str/ item / edit /(ctxt:id))))

通过将ID附加到现有网址,将网址嵌入我的代码中。

 (html / defsnippet fooviews / foo .html[:#main] 
[ctxt]
[:a @ href](html / append(ctxt:id)))


解决方案

@ddk answer是现货,但你可能更喜欢一个更通用的方法来解决问题

 (更新更新节点[: attrs attr] f args))))

,然后

 (update-attr:href str123)


Is it possible to append a value to an attribute using enlive?

example: I have this

<a href="/item/edit/">edit</a>

and would like this

<a href="/item/edit/123">edit</a>

I am currently doing this:

(html/defsnippet foo "views/foo.html" [:#main]
  [ctxt]
  [:a] (html/set-attr :href (str "/item/edit/" (ctxt :id))))

But I would prefer not to embed the URL into my code, by just appending the id to the existing URL

(html/defsnippet foo "views/foo.html" [:#main]
  [ctxt]
  [:a@href] (html/append (ctxt :id)))

解决方案

@ddk answer is spot on but you may prefer a more generic way to solve the problem

(defn update-attr [attr f & args]
    (fn [node]
      (apply update-in node [:attrs attr] f args))))

and then

(update-attr :href str "123")

这篇关于追加到Enlive中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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