stripping HTML out of templates?

Options

I'm working on a list template for one of our content types, and it doesn't appear that there's any way to strip HTML code out of a field.  I'm trying to do a shortened blurb for each item, but unfortunately some of our item descriptions have HTML in them (including images), so the blurbs don't look very uniform.  Any ideas on how to solve this?

Thanks,

Seth

Tagged:

Comments

  • I actually tried this using PHP. While I got the process theoretically working, the HTML code CMS produces is so badly broken that around half the time, PHP could not effectively strip the tags out without mangling the text as well.

    If anyone does find an efficient way to do this, please count me in on wanting a solution

  • Would it be sufficient if there was a stripHTML(string) function that turned:

    <p>This is some random <a href="link.html">html</a></p>

    <img src="/path/to/image.png">

    <div id="foo" class="bar">This has a <span>span tag</span> in it!</div>

    into:

    This is some random html


    This has a span tag in it!

    Message was edited by: BruceKeilin: Tried to fix raw HTML.

  • Jim Drey:

    I actually tried this using PHP. While I got the process theoretically working, the HTML code CMS produces is so badly broken that around half the time, PHP could not effectively strip the tags out without mangling the text as well.

    If anyone does find an efficient way to do this, please count me in on wanting a solution

    Not that I think it is entirely recommendable, you could do this through JS

    <!-- you could hide it first, so there is no unsightly flash -->

    <div id="list" style="display:none;">

         <div id="blurb1" class="listitem"><a href="nonn">be right back</a>here is a proces that I would like to see<p>tthat iw ould teach the of things to be</div>

         <div id="blurb2" class="listitem"><a href="nonn">be right back</a>here is a proces that I would like to see<p>tthat iw ould teach the of things to be</div>

    </div>

    <script>

    stripBlurb = function( listitem ) {

              if (listitem.innerText) {

                        listitem.innerHTML = listitem.innerText;

              } else {

                        listitem.innerHTML = listitem.innerHTML.replace(/(<(+)>)/ig,"")

              }

    }

    var blurblist = document.getElementById("list")

    var blurbs = blurblist.getElementsByTagName("DIV")

    for (var x = 0; x < blurbs.length; x++ ) {

              if (blurbs.className == "listitem") stripBlurb(blurbs);

    }

    blurblist.style.display = "block";

    </script>

  • Bruce Keilin:

    Would it be sufficient if there was a stripHTML(string) function that turned:

    <p>This is some random <a href="link.html">html</a></p>

    <img src="/path/to/image.png">

    <div id="foo" class="bar">This has a <span>span tag</span> in it!</div>

    into:

    This is some random html


    This has a span tag in it!

    Message was edited by: BruceKeilin: Tried to fix raw HTML.

    Yes, extremely sufficient (in that it would solve the issue).

  • It's coming in tomorrow evening's release! There will be a new function toText(html,tags) that takes a chunk of HTML and a comma-separated list of allowed tags. So,



    <t:set id="stripped" value="toText(body,'p,em')"/>

    Should give you a local variable named "stripped" that contains the page body HTML with only P and EM tags in it.

    Let me know how this works out for you, Seth.

    Message was edited by: BruceKeilin: changed "valued" to "value"

  • Bruce Keilin:

    It's coming in tomorrow evening's release! There will be a new function toText(html,tags) that takes a chunk of HTML and a comma-separated list of allowed tags. So,



    <t:set id="stripped" value="toText(body,'p,em')"/>

    Should give you a local variable named "stripped" that contains the page body HTML with only P and EM tags in it.

    Let me know how this works out for you, Seth.

    Message was edited by: BruceKeilin: changed "valued" to "value"

    Bruce,

    It works great (though I only tested using an empty set of tags '') once I change "valued" to "value".

    Thanks for making this happen, it's a great feature.



    Seth

  • Seth Leonard:

    Bruce,

    It works great (though I only tested using an empty set of tags '') once I change "valued" to "value".

    Thanks for making this happen, it's a great feature.



    Seth

    Glad it was helpful (and thanks for the proof-reading. I've updated the source.)

Categories