Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » [Solved] limitcharacters is stripping html

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1903 Views
  • w1nk5
    Avatar
    Community Member
    25 Posts

    [Solved] limitcharacters is stripping html Link to this post

    I have 3 featured columns on the homepage of a project I am working on. I am limiting the number of characters it returns for consistency. My code is as follows.

    $Content.LimitCharacters(285, ...)

    For whatever reason it is returning the content without the p tags which is the only html in the content. So the first 2 paragraphs become one and it is quite ugly.

    I need those p tags!!!

    Any ideas?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: [Solved] limitcharacters is stripping html Link to this post

    Well...

    It does say in the documentation (and quite clearly in the php code if you were to open '\sapphire\core\model\fieldtypes\HTMLText.php') of the function that it will strip tags. I can see why this is required due to limiting the number of words... if you counted words within script tags you would get a bad result. And the logic of maintaining the html and counting words id not that easy to do.

    You could write your own function if you want to only count words not in tags. As a quick and dirty solution I might use...
    1) replace your </p> & <br> with something like {ENDLINE}
    2)do the same logic as LimitCharacters
    3)Replace {ENDLINE} with <br>

    Barry

  • w1nk5
    Avatar
    Community Member
    25 Posts

    Re: [Solved] limitcharacters is stripping html Link to this post

    But I don't want to only count words and not in tags. I want it to return the words and the tags.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: [Solved] limitcharacters is stripping html Link to this post

    If I misunderstood I'm sorry...

    Regarding the function... it clearly strips the tags and the reason for doing so is obvious... it is not easy to count the number of words while there are tags in there... really it shouldn't take more than 5 minutes to create the function yourself based on my simple suggestion... why not do that?

    You can raise this as a ticket on open.silverstripe.org as an enhancement... but it is my very strong suspicion it would be quicker to do it yourself Or how about doing it yourself AND sharing it with us?

    Barry

    1903 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.