Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

[Solved] limitcharacters is stripping html


Reply


4 Posts   2185 Views

Avatar
w1nk5

Community Member, 25 Posts

13 July 2010 at 4:03am

Edited: 16/07/2010 4:49am

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?

Avatar
swaiba

Forum Moderator, 1805 Posts

13 July 2010 at 9:17am

Edited: 13/07/2010 10:14am

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

Avatar
w1nk5

Community Member, 25 Posts

13 July 2010 at 9:40am

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

Avatar
swaiba

Forum Moderator, 1805 Posts

13 July 2010 at 10:03am

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