bibsch Posted January 6, 2019 Posted January 6, 2019 hello community im using this command echo $sanitizer->truncate($child->body, $maxLength = 200, $options = [ 'keepFormatTags' => true, ]); i want to keep my body wrapped with <p>. what im doing wrong
adrian Posted January 6, 2019 Posted January 6, 2019 According to the docs it doesn't keep <p> tags: https://processwire.com/blog/posts/processwire-3.0.101-core-updates/ Quote keepFormatTags (BOOLEAN) When true, this keeps all HTML text formatting tags in the returned string, stripping only the block level ones. The tags it leaves are: abbr, acronym, b, big, cite, code, em, i, kbd, q, samp, small, span, strong, sub, sup, time, and var. What you want is keepTags so you can specify the tags to keep - I think the only catch is that you'll need to specify a lot more than just 'p' like in my example. Perhaps keepFormatTags should include <p> - maybe something to mention to Ryan on Github?
bibsch Posted January 6, 2019 Author Posted January 6, 2019 http://84.151.221.133/q2jump/blog/ <?php include("./head.inc"); foreach($page->children() as $child) { echo "<h3>$child->title</h3>"; echo $page->createdUser->name; echo $sanitizer->truncate($child->body, $maxLength = 200, $options = [ 'keepTags' => array('p'), ]); echo "<a href='$child->url'>Read more...</a><hr>"; } include("./foot.inc"); whats happens... only my last child body is wrapped in <p>
Zeka Posted January 6, 2019 Posted January 6, 2019 I this test case 'truncate' method also removes paragraph tags. I have tried different combinations of 'keepTags' and 'keepFormatTags' without luck.
Robin S Posted January 6, 2019 Posted January 6, 2019 2 hours ago, Zeka said: I this test case 'truncate' method also removes paragraph tags. I have tried different combinations of 'keepTags' and 'keepFormatTags' without luck. I tested it too, and the problem occurs when the paragraph is longer than the truncate length, causing there to be no closing </p> tag present in the truncated string. There is an option for the fixUnclosedTags() method used by truncate, to close instead of remove unclosed tags... * Fix/close unclosed tags: * ------------------------ * When the remove option is false, it will attempt to close unclosed tags rather than * remove them. It doesn't know exactly where they should be closed, so it appends the * close tags to the end of the string. ...but the truncate method calls fixUnclosedTags() with remove = true and doesn't provide a way to do otherwise. I guess you could use WireTextTools::fixUnclosedTags() before calling $sanitizer->truncate but it would be nice to have the option built into the truncate method. Maybe an oversight by Ryan? Edit: request opened here: https://github.com/processwire/processwire-requests/issues/253 @bibsch, because your truncate length is short and will only contain a single paragraph you can achieve your objective by adding <p> tags around your truncated text: echo '<p>' . $sanitizer->truncate($child->body, 200) . '</p>'; 5
Stefanowitsch Posted August 25, 2023 Posted August 25, 2023 On 1/6/2019 at 10:46 PM, Robin S said: I tested it too, and the problem occurs when the paragraph is longer than the truncate length, causing there to be no closing </p> tag present in the truncated string. There is an option for the fixUnclosedTags() method used by truncate, to close instead of remove unclosed tags... * Fix/close unclosed tags: * ------------------------ * When the remove option is false, it will attempt to close unclosed tags rather than * remove them. It doesn't know exactly where they should be closed, so it appends the * close tags to the end of the string. ...but the truncate method calls fixUnclosedTags() with remove = true and doesn't provide a way to do otherwise. I guess you could use WireTextTools::fixUnclosedTags() before calling $sanitizer->truncate but it would be nice to have the option built into the truncate method. Maybe an oversight by Ryan? Edit: request opened here: https://github.com/processwire/processwire-requests/issues/253 @bibsch, because your truncate length is short and will only contain a single paragraph you can achieve your objective by adding <p> tags around your truncated text: echo '<p>' . $sanitizer->truncate($child->body, 200) . '</p>'; I am stumbling over and over again over this issue and I really think this should be fixed, or at least the 'remove' option of the fixUnclosedTags() Function should be adjustable in the $sanitizer->truncate() method. @ryan
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now