Search code examples
clojureenlive

enlive remove html tag


I have this html snippet. I want parse that snippet and emit html without javascript tags

<html>
    <body>
        <div class="content">lorem ipsum</div>
        <script  src="/js/jquery.js" type="text/javascript"></script>
        <script src="/js/bootstrap.min.js" type="text/javascript"></script>
    </body>
</html>

become this

<html>
    <body>
        <div class="content">lorem ipsum</div>
    </body>
</html>

I couldn't found enlive helper function to remove tags.

I have found the solution thanks for the example. So I write this code and the js disappeared

(html/deftemplate template-about "../resources/public/build/about/index.html"
    []
    [:script] (fn js-clear [& args] nil)
)

Solution

  • My usual approach when I need to conditionally remove some tags of the rendered page is to use a nil returning function.

    For instance

    (html/defsnippet upgrade-plan "page_templates/upgrade-plan.html" [:#upgradePlanSection]
      [pending-invoice ... ]
      ...
      [:#delayedPlanWarning] #(when pending-invoice
                                (html/at %
                                         [:#delayedPlanWarning] (html/remove-attr :style)
                                         [:.messagesText] (html/html-content (tower/t :plans/pending-invoice 
                                                                                    (:id pending-invoice)))))
      ...
    

    In that particular case if pending-invoice is nil the delayedPlanWarning element is removed from the rendered html since the function returns nil.