首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何改进这个Enlive模板?

如何改进这个Enlive模板?
EN

Stack Overflow用户
提问于 2012-12-29 22:48:56
回答 1查看 469关注 0票数 1

我使用这个Enlive模板将它下面的HTML转换为它下面的HTML。基于twitter名称的集合,我生成了一个包含链接的表。我怎样才能摆脱enlive/clone-for内部的卡顿?

代码语言:javascript
复制
(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输入

代码语言:javascript
复制
<!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输出

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-06 02:32:24

您可以从以下位置更改tbody.tr模板:

代码语言:javascript
复制
<tr>
  <td>name</td>
</tr>

至:

代码语言:javascript
复制
<tr>
  <td><a href="https://twitter.com/intent/user?screen_name=foo">foo</a></td>
</tr>

现在,您的HTML资源就是您想要的输出的工作示例。

然后修改您的deftemplate以支持它:

代码语言:javascript
复制
(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=,然后把代码改成类似于:

代码语言:javascript
复制
                    (enlive/do->
                     (fn [node] (update-in node [:attrs :href] #(str % username)))
                     (enlive/content username))))

你也可以让它成为一个函数。例如,参见Append to an attribute in Enlive

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14082758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档