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.

Archive /

Our old forums are still available as a read-only archive.

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

HTMLText->Summary() does not work


Go to End


3 Posts   2509 Views

Avatar
xmedeko

Community Member, 94 Posts

25 May 2007 at 12:37pm

Hi,
when I use $Content.Summary(20) it always shows me the first paragraph, however long it is. When I have deleted this method from HTMLText, then is used Text->Summary(20) and it does what I expect.

BTW. Why HTMLText is overloading only the Summary method, and not other ones like FirstSentence, FirstParagraph, BigSummary, ... ?

Avatar
xmedeko

Community Member, 94 Posts

25 May 2007 at 1:13pm

OK, I see HTMLText->Summary preserver HTML tags. So, here's my path:

--- HTMLText.php.orig 2007-05-25 13:00:18.000000000 +1200
+++ HTMLText.php 2007-05-25 13:09:17.000000000 +1200
@@ -33,13 +33,12 @@
}
elseif( preg_match( '/<\/(\w+)>/', $parts[$pIndex], $endTag ) && $endTag[1] == substr( $tagStack[count($tagStack) - 1], 1, strlen( $endTag[1] ) ) ) {
array_pop( $tagStack );
- $words++;
$summary .= $parts[$pIndex++];
} elseif( preg_match( '/^<\w+/', $parts[$pIndex] ) ) {
array_push( $tagStack, $parts[$pIndex] );
- $words++;
$summary .= $parts[$pIndex++];
} else {
+ $words++;
$summary .= $parts[$pIndex++] . ' ';
}
}
@@ -52,7 +51,11 @@
if(sizeof($tagName) > 0)
$summary .= "</{$tagName[1]}>";
}
-
+ // add triple dot and close the paragraph, if the paragraph has been trimmed
+ if ( $words > $maxWords || $pIndex >= count( $parts ) ) {
+ $summary .= '...</p>';
+ }
+
return $summary;
}

Now, it seems to work well. Anyway, it would be nice to have other similar text manipulation methods from Text class also in HTMLText class.

Avatar
xmedeko

Community Member, 94 Posts

25 May 2007 at 2:58pm

Hmm, but if there's a picture in the first paragraph, like

<p><img ...></img></p>

then it shows only that picture. I am going to use the Text::Summary method.