Reputation: 15
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)
)
Upvotes: 1
Views: 1127
Reputation: 5231
When removing whole tags, you only need to use nil
as your transformation.
(deftemplate tester1
(java.io.StringReader. "<html><body><div class=\"content\">...")
[]
[:script] nil)
(template1)
In your case, you would replace the StringReader
with the resource you are using.
Upvotes: 0
Reputation: 210
If you don't mind the extra parsing and emitting, the following would do nicely:
(def orig (html-resource (java.io.StringReader. "<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>")))
(def stripped (transform orig [(keyword "script")] (substitute "")))
(apply str (emit* stripped))
Upvotes: 0
Reputation: 4702
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
.
Upvotes: 2