Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

truncate

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
gtgibson
Smarty Rookie


Joined: 30 Jul 2003
Posts: 9
Location: New Zealand

PostPosted: Wed Jul 30, 2003 8:50 am    Post subject: truncate Reply with quote

Is there a way using smarty to truncate at the first instance of a character or string? Obviously easy enough to do in PHP using a combiantion of substr() and strstr(), but would like to do this from a template.

specifically, I want to show an article and show it as a truncated version, cut off after the first paragraph. So i want to truncate at the first in stance of '\n'. Any ideas?

Obvioulsy in this instance, if '\n' is not found, it should display the whole string.

Cheers

GG
_________________
I suck at web development.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jul 30, 2003 9:41 am    Post subject: Reply with quote

{$text|regex_replace:"/(.*?)\n.*$/s":"\$1"}

but maybe it's worth to write a little modifier to encapsulate this:

[modfier.first_paragraph.php][php:1:c79588648c]<?php
function smarty_modifier_first_paragraph($str, $delim="\n") {
return preg_replace('/^(.*?)'.preg_quote($delim).'.*$/s', '$1', $str);

}

?>[/php:1:c79588648c]

or maybe with strpos and substr, YMMV.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
gtgibson
Smarty Rookie


Joined: 30 Jul 2003
Posts: 9
Location: New Zealand

PostPosted: Wed Jul 30, 2003 12:10 pm    Post subject: Reply with quote

OK, your first solution will be fine.

Quote:

but maybe it's worth to write a little modifier to encapsulate this:


how do you use register_modifier?
Where is that supposed to go in my code?

P.S. Wo arbeitest du in Oldenburg? (Ich bin Neuseelander, aber wohne jetzt in Westerstede, und seit 6 monate arbeite ich in Oldenburg).
_________________
I suck at web development.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jul 30, 2003 12:21 pm    Post subject: Reply with quote

you don't need to register the modifier. put it in a file called "modifier.[modifier_name].php" in your plugins-directory and ensure the function in it is named "smarty_modifier_[modifier_name]". smarty will autoload and register it for you as soon as a template uses it. Smile

(ich arbeite bei dpo ( www.dpo.de ) direkt in der "city" Smile )
Back to top
View user's profile Send private message Send e-mail Visit poster's website
gtgibson
Smarty Rookie


Joined: 30 Jul 2003
Posts: 9
Location: New Zealand

PostPosted: Wed Jul 30, 2003 12:46 pm    Post subject: Reply with quote

unser Hauptstadt Wink
_________________
I suck at web development.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP