1

我使用这个 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>
4

1 回答 1

1

您可以从以下位置更改 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))))

你也可以利用它。参见例如附加到 Enlive 中的属性

于 2013-01-05T18:32:23.883 回答