我使用这个Enlive模板将它下面的HTML转换为它下面的HTML。基于twitter名称的集合,我生成了一个包含链接的表。我怎样才能摆脱enlive/clone-for内部的卡顿?
(enlive/deftemplate usernames-table-body
"public/whoisnotfollowingme.html"
[usernames]
[:table.names :tbody :tr]
(enlive/clone-for [username usernames]
[:td]
(enlive/html-content
(html [:a {:href (str "https://twitter.com/intent/user?screen_name=" username)} username]))))HTML输入
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/bootstrap/css/bootstrap.css"/>
</head>
<body>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
<div class="container">
<div class="hero-unit">
<p class="names">These people who you are following are not following you back!</p>
</div>
<table class="names table table-striped">
<thead>
<tr>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>HTML输出
<html>
<head>
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<script src="//platform.twitter.com/widgets.js" type="text/javascript"></script>
<div class="container">
<div class="hero-unit">
<p class="names">These people who you are following are not following you back!</p>
</div>
<table class="names table table-striped">
<thead>
<tr>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td> < a href =" https://twitter.com/intent/user?screen_name=foo " > foo </ a > </td>
</tr> <tr>
<td> < a href =" https://twitter.com/intent/user?screen_name=bar " > bar </ a > </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>发布于 2013-01-06 02:32:24
您可以从以下位置更改tbody.tr模板:
<tr>
<td>name</td>
</tr>至:
<tr>
<td><a href="https://twitter.com/intent/user?screen_name=foo">foo</a></td>
</tr>现在,您的HTML资源就是您想要的输出的工作示例。
然后修改您的deftemplate以支持它:
(enlive/deftemplate usernames-table-body
"public/whoisnotfollowingme.html"
[usernames]
[:table.names :tbody :tr]
(enlive/clone-for [username usernames]
[:td :a]
(enlive/do->
(enlive/set-attr :href (str "https://twitter.com/intent/user?screen_name=" username))
(enlive/content username))))编辑:如果你想去掉代码中的URL,试着把你的href改成?screen_name=,然后把代码改成类似于:
(enlive/do->
(fn [node] (update-in node [:attrs :href] #(str % username)))
(enlive/content username))))你也可以让它成为一个函数。例如,参见Append to an attribute in Enlive。
https://stackoverflow.com/questions/14082758
复制相似问题