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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

NoHTML in LimitWordCount Summary


Go to End


10 Posts   11583 Views

Avatar
Devel0per

Community Member, 5 Posts

14 December 2010 at 8:07pm

u can add given below function to ur "sapphire/core/model/fieldtypes/Text.php

function CustomWordCount($numWords = 30, $add = '...') {
$this->value = strip_tags($this->value);
$ret = explode(' ', $this->value, $numWords + 1);

if(count($ret) <= $numWords - 1) {
$ret = $this->value;
} else {
array_pop($ret);
$ret = implode(' ', $ret) . $add;
}
return $ret;
}

and then in .ss file call,
$Content.CustomWordCount

i hope it will work for u.

Avatar
Johan

Community Member, 49 Posts

14 January 2011 at 12:06am

Edited: 14/01/2011 12:08am

This is new I think.

I see this in "sapphire/core/model/fieldtypes/Text.php"

/**
* Limit this field's content by a number of words.
* CAUTION: This is not XML safe. Please use
* {@link LimitWordCountXML()} instead.
*
* @param int $numWords Number of words to limit by
* @param string $add Ellipsis to add to the end of truncated string
* @return string
*/
function LimitWordCount($numWords = 26, $add = '...') {
$this->value = trim(Convert::xml2raw($this->value));
$ret = explode(' ', $this->value, $numWords + 1);

if(count($ret) <= $numWords - 1) {
$ret = $this->value;
} else {
array_pop($ret);
$ret = implode(' ', $ret) . $add;
}

return $ret;
}

So add $Content.LimitWordCount to the .ss file for 26 words or add $Content.LimitWordCount(10) to change the amount of words.

Go to Top