• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [SOLVED] Smarty Truncate Problem
PostPosted: Sun Jul 08, 2012 10:53 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Fri Nov 28, 2008 11:26 am
Posts: 103
Location: London, UK
So this is a strange one...

I have a news summary that looks like this:

Code:
{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:
<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.

Top
 Profile  
 
 Post subject: Re: Smarty Truncate Problem
PostPosted: Mon Jul 09, 2012 12:37 am 
Offline
Administrator
Administrator
User avatar

Joined: Thu Mar 09, 2006 5:32 am
Posts: 11812
Location: Arizona
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...

_________________
Extensions » Modules/Tags click the name of the module/tag or Help to the right to get its parameters.
Right click and view source is a great way to see what you have to work with.
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


Top
 Profile  
 
 Post subject: Re: Smarty Truncate Problem
PostPosted: Mon Jul 09, 2012 2:13 am 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Tue Oct 19, 2004 6:44 pm
Posts: 6585
Location: Fernie British Columbia, Canada
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
--
if you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
----------------
Don't make me angry..... you won't like me when I'm angry....


Top
 Profile  
 
 Post subject: Re: Smarty Truncate Problem
PostPosted: Mon Jul 09, 2012 10:22 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Fri Nov 28, 2008 11:26 am
Posts: 103
Location: London, UK
Perfect...

Code:
|strip_tags|truncate


Worked a treat! Thank you.


Top
 Profile  
 
 Post subject: Re: [SOLVED] Smarty Truncate Problem
PostPosted: Thu Jul 12, 2012 12:32 am 
Offline
Power Poster
Power Poster

Joined: Fri Mar 30, 2007 2:28 am
Posts: 860
Location: London
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:
<?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: */

?>


Top
 Profile  
 
 Post subject: Re: [SOLVED] Smarty Truncate Problem
PostPosted: Sat Jul 14, 2012 7:37 am 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Wed Apr 23, 2008 7:53 am
Posts: 5083
Location: The Netherlands
Nice one, Applejack!

_________________
Dutch CMSMS community website cmsms.nl and Wiki wiki.cmsms.nl
--------------------------
My CMSMS Tips and Tricks Weblog and website about Pneumatic Tube Systems / Rohrpost
My other (Dutch) website: Smakelijk eten zonder zout (Design: Compufairy)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
A2 Hosting