【问题标题】:How to improve this Enlive template?如何改进这个 Enlive 模板?
【发布时间】:2013-01-06 03:28:34
【问题描述】:

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

【问题讨论】:

    标签: clojure enlive


    【解决方案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))))
    

    您也可以使用它。参见例如Append to an attribute in Enlive.

    【讨论】:

    • 酷,这就是我正在寻找的。一种可能的改进:将链接保留在 html 中,仅将用户名附加到它。如何在 Enlive 中做到这一点?
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多