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

Add a line break after 3 words

 
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
Aurelou
Smarty n00b


Joined: 23 Mar 2021
Posts: 2

PostPosted: Tue Mar 23, 2021 10:18 am    Post subject: Add a line break after 3 words Reply with quote

Hello,

Here is my actual code with truncate:

Code:
<a href="{$product.url}">{$product.name|truncate:50:'...'}</a>


I want to change it, I don't want to use truncate, I need to add a line break in $product.name after 3 words.

I found this php code which allows to do it :

Code:
<?php
$test = 'one two three four five six seven eight nine ten eleven tweleve thirteen fourteen';
echo preg_replace('~((\w+\s){3})~', '$1' . "\n", $test);
?>


Can you please help me translate it into smarty in order to make it work with my code? I think in smarty it's not preg_replace but regex_replace. Or if you have another solution to make a line break after 3 words...

Thank you very much for your help, I am a novice.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Mar 24, 2021 4:28 am    Post subject: Reply with quote

Try:
Code:
{$name_array = " "|explode:$product.name}{foreach $name_array as $name}{if $name@index eq 3}<br>{/if}{$name} {/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Mar 24, 2021 7:02 pm    Post subject: Reply with quote

Code:
{if empty($name@iteration % 3)}<br/>{/if}

Ref: https://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.iteration
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Mar 24, 2021 10:21 pm    Post subject: Reply with quote

So, this is MOD(3)?

This will break after every third word.
Back to top
View user's profile Send private message
Aurelou
Smarty n00b


Joined: 23 Mar 2021
Posts: 2

PostPosted: Thu Mar 25, 2021 1:53 pm    Post subject: Reply with quote

A BIG thank you, it works perfectly!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Mar 25, 2021 2:15 pm    Post subject: Reply with quote

bsmither wrote:
So, this is MOD(3)?

This will break after every third word.


Which was exactly the intent.
Back to top
View user's profile Send private message
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