|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
gtgibson Smarty Rookie
Joined: 30 Jul 2003 Posts: 9 Location: New Zealand
|
Posted: Wed Jul 30, 2003 8:50 am Post subject: truncate |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Jul 30, 2003 9:41 am Post subject: |
|
|
{$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 |
|
gtgibson Smarty Rookie
Joined: 30 Jul 2003 Posts: 9 Location: New Zealand
|
Posted: Wed Jul 30, 2003 12:10 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Jul 30, 2003 12:21 pm Post subject: |
|
|
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.
(ich arbeite bei dpo ( www.dpo.de ) direkt in der "city" ) |
|
Back to top |
|
gtgibson Smarty Rookie
Joined: 30 Jul 2003 Posts: 9 Location: New Zealand
|
Posted: Wed Jul 30, 2003 12:46 pm Post subject: |
|
|
unser Hauptstadt _________________ I suck at web development. |
|
Back to top |
|
|
|
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
|