[SOLVED] Smarty Truncate Problem

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
User avatar
beherenow_uk
Forum Members
Forum Members
Posts: 103
Joined: Fri Nov 28, 2008 11:26 am
Location: London, UK

[SOLVED] Smarty Truncate Problem

Post by beherenow_uk »

So this is a strange one...

I have a news summary that looks like this:

Code: Select all

{if $entry->content}
<div class="newstext">
{eval var=$entry->content|truncate:100}
<p><a class="readmore" href="{$entry->moreurl}">read more &rarr;</a></p>
</div>
{/if}
My <p> tags are inside the article, which outputs:

Code: Select all

<div class="newstext">
<p>As part of our commitment to ensure you reach your personal Health Goals, (company name) have...
<p><a class="readmore" href="#thelink">read more &rarr;</a></p>
</div>
Problem is, as you can see the trailing </p> is truncated too, meaning that the page doesn't validate! And I potentially will have display issues too.

Anybody have an idea how to make it NOT truncate the trailing </p> tag? I've looked everywhere!

Thanks in advance.
Last edited by beherenow_uk on Tue Jul 10, 2012 11:18 pm, edited 1 time in total.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12678
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: Smarty Truncate Problem

Post by Dr.CSS »

The problem is not knowing where the <p> is going to end, if it has 2 paragraphs or ? but you could add </p> after the summary truncate call in the summary template...
Check ver. CMSMS, PHP, server OS, in System Information page.
Default content http://multiintech.com/defaultcontent/
People are Wonderful
Business is Great
Life is Terrific
Ever wonder what happened to the Album module? Well it is alive and well.
http://album.multiintech.com/
Image
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Smarty Truncate Problem

Post by calguy1000 »

The summarize, and truncate modifiers are designed to work with text, not html. They count characters, or words... they don't care about what markup is there.

If you want to display the first N characters of 'text' you have to convert the html to text first.

|strip_tags|truncate

should do the trick.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
beherenow_uk
Forum Members
Forum Members
Posts: 103
Joined: Fri Nov 28, 2008 11:26 am
Location: London, UK

Re: Smarty Truncate Problem

Post by beherenow_uk »

Perfect...

Code: Select all

|strip_tags|truncate 
Worked a treat! Thank you.
applejack
Power Poster
Power Poster
Posts: 1015
Joined: Fri Mar 30, 2007 2:28 am
Location: London

Re: [SOLVED] Smarty Truncate Problem

Post by applejack »

If you want to truncate just the first sentence you can use the modifier below which I recently came across. Just copy the code and upload to /lib/smarty/plugins name it modifier.first_sentence.php

In you smarty tag just use |first_sentence

Calguy how about adding this as one the core tags as it is pretty useful.

Code: Select all

<?php 
/** 
 * Smarty plugin 
 * @package Smarty 
 * @subpackage plugins 
 */ 


/** 
 * Smarty first_sentence modifier plugin 
 * 
 * Type:     modifier
 * Name:     first_sentence 
 * Purpose:  trim a string to the first sentence 
 * @link http://www.phpinsider.com/smarty-forum/viewtopic.php?t=10170 
 * @author   Boots 
 * @param string 
 * @return integer 
 */ 
function smarty_modifier_first_sentence($string) 
{ 
    // find periods with a word before but not after. 
    preg_match('/^.*[^\s]\.(?!\w)/U', $string, $match); 
    return $match[0]; 
} 

/* vim: set expandtab: */ 

?>
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7776
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: [SOLVED] Smarty Truncate Problem

Post by Rolf »

Nice one, Applejack!
Image
- + - + - + - + - + -
Latest CMSMS tutorial: FormBuilder WatchGuard
- + - + - + - + - + -
Did my post help you solving a problem at your (customers) website and it saved you many hours of work?
Great!! Buy me a cup of coffee in return as a small token of appreciation!
Post Reply

Return to “CMSMS Core”