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

Adding spaces between elements of a

 
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
Martine
Smarty Rookie


Joined: 14 Jul 2018
Posts: 5

PostPosted: Sat Jul 14, 2018 6:50 am    Post subject: Adding spaces between elements of a Reply with quote

Hello,

I have this code where a list of extras if checked gets listed on a given page. I would like to have a comma and a space after each extra, if not last extra in the list

Code:

            {if !empty($item.extras)}
            <div>
                 <span>{lang key='field_listings_extras'}:</span>
                 <span>{$item.extras}</span>
            </div>
             {/if}


Thank you in advance
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Jul 14, 2018 2:52 pm    Post subject: Reply with quote

An example of sourse data and the desired output could help better understand what you want.
Back to top
View user's profile Send private message
Martine
Smarty Rookie


Joined: 14 Jul 2018
Posts: 5

PostPosted: Sat Jul 14, 2018 4:54 pm    Post subject: Reply with quote

I am very newbie to this I have a subrion cms and this is in a view listing tpl page.

What do you mean an example of souce code?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jul 15, 2018 5:18 pm    Post subject: Reply with quote

Where did I say "source code" ?
Back to top
View user's profile Send private message
Martine
Smarty Rookie


Joined: 14 Jul 2018
Posts: 5

PostPosted: Sun Jul 15, 2018 7:45 pm    Post subject: Reply with quote

Yes oops

The source data is php sql I don't know if that helps
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Jul 16, 2018 4:35 pm    Post subject: Reply with quote

I don't need to know where it comes from, I need to see how it looks.
And what the intended result from that data.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


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

PostPosted: Sat Jul 21, 2018 3:20 am    Post subject: Reply with quote

See:
https://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.last

We assume that $item is from an array of $items.

Therefore, using the foreach property of @last can determine if you should or should not add more content between each $item.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Jul 21, 2018 10:01 am    Post subject: Reply with quote

Judging by his hints, he don't need foreach for that.
Back to top
View user's profile Send private message
Martine
Smarty Rookie


Joined: 14 Jul 2018
Posts: 5

PostPosted: Tue Jul 24, 2018 7:28 pm    Post subject: Reply with quote

Hello,

found it

Code:
{if !empty($item.extras)}
                                    <div>
                                        <span>{lang key='field_listings_extras'} :</span>
                                        <span>
                                           {foreach $item.extras_formatted as $extra}
                                              {lang key="field_listings_extras+$extra"}{if !$extra@last}, {/if}
                                           {/foreach}
                                        </span>
                                    </div>
                                {/if}


Thank you
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jul 25, 2018 3:35 pm    Post subject: Reply with quote

Why not just
Code:
{", "|join:$item.extras_formatted}
?
Back to top
View user's profile Send private message
Martine
Smarty Rookie


Joined: 14 Jul 2018
Posts: 5

PostPosted: Wed Jul 25, 2018 8:03 pm    Post subject: Reply with quote

Hello,

I tried, on another field, it did not work. The code I found working, does not put a comma at the end Wink
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Jul 27, 2018 1:57 am    Post subject: Reply with quote

I'm not sure, what you mean by that.
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