• 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 This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 
Author Message
 Post subject: Show items in a random order, shuffle your Smarty arrays!
PostPosted: Thu Sep 09, 2010 9:08 pm 
Offline
Support Guru
Support Guru
User avatar

Joined: Wed Sep 05, 2007 8:03 pm
Posts: 4000
Location: The Netherlands
Many modules have ways to output lists of items. In most of them the output can be customized in "summary templates".

If you would like to display the items in a random order, some modules provide a parameter you can set. But if a module lacks a randomize parameter, there is also a simple way to accomplish this with a single line of Smarty code.

Let's look at the News module
In the summary template you'll find the Smarty array variable $items which in fact contains all the news-items. You'll also recognize the line
{foreach from=$items item=entry} which is used to loop over the $items array.

To show the newsitems in a random order you just put this code right before the {foreach from=$items item=entry} statement

Code:
{capture}{$items|@shuffle}{/capture}


I used a capture around the actual shuffle function to hide the function-output '1'. Maybe there is a simpler way to do that, but this was my first thought.

Another example: Gallery
The Gallery module has a parameter action='showrandom', but if you just want the images of the specific subgallery shuffled, then you can use

Code:
{capture}{$images|@shuffle}{/capture}


right before the {foreach from=$images item=image} statement

You can use this in every template where you see an array in the {foreach} or {section} tag.

_________________


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays!
PostPosted: Thu Sep 09, 2010 9:14 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Tue Oct 24, 2006 5:59 pm
Posts: 906
Location: Mandan, ND
I like when tips are simple and effective! :) Thanks Jos!

_________________
If all else fails, use a bigger hammer.

M@rtijn wrote:
This is a community. This means that we work together and have the same goal (a beautiful CMS), not that we try to put people down and make their (voluntary) job as difficult as can be.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays!
PostPosted: Thu Sep 09, 2010 10:01 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Fri Feb 02, 2007 4:31 pm
Posts: 2385
Location: Comox Valley, BC
Nice tip, Jos.

Nullig


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays!
PostPosted: Fri Sep 10, 2010 7:19 am 
Offline
Power Poster
Power Poster
User avatar

Joined: Sat Aug 25, 2007 9:55 am
Posts: 1150
Location: Berlin
:) ;) :D


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays!
PostPosted: Mon Sep 13, 2010 12:09 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Sat Aug 25, 2007 9:55 am
Posts: 1150
Location: Berlin
jos, do you have an idea how to sort the foreach output by filename? maybe with a modifier for smarty? all i found was this thread in smarty forum but i have problems installing it.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays!
PostPosted: Mon Sep 13, 2010 1:43 pm 
Offline
Support Guru
Support Guru
User avatar

Joined: Wed Sep 05, 2007 8:03 pm
Posts: 4000
Location: The Netherlands
For Gallery you can set the sorting on filename in the template settings. But I guess you're not refering to Gallery here.

In the thread you found, they try to code a custom Smarty modifier with php. That is way out of scope in this Tip&Trick

_________________


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays
PostPosted: Fri Oct 10, 2014 3:36 pm 
Offline
Forum Members
Forum Members

Joined: Tue Feb 28, 2012 1:24 pm
Posts: 176
sorry but i have a problem

I have posted on the site about 50 news

in summary template i this code

{capture}{$items|@shuffle}{/capture}
{foreach from=$items item=entry}


in the page template i have
{news number='6' summarytemplate="carosello"}


displaying random works but always mixed the last 6 items
I would like to view 6 articles published between all 50

thanks for help

_________________
http://www.gianlucacompagno.com/


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays
PostPosted: Fri Oct 10, 2014 8:29 pm 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Wed Apr 23, 2008 7:53 am
Posts: 7690
Location: The Netherlands
Remove number parameter from news tag and add counter/break code like I added in this blog https://www.cmscanbesimple.org/blog/a-x ... ain-smarty

_________________
Image

Did my post help you solving a problem at your (customers) website and it saved you many hours of work? Great!! Consider buying me a cup of coffee in return!



Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays
PostPosted: Fri Oct 10, 2014 8:55 pm 
Offline
Forum Members
Forum Members

Joined: Tue Feb 28, 2012 1:24 pm
Posts: 176
thanks for replay

but i need to view max 6 random news in a page

example

http://www.teneriecoccolosi.it/news/62/16/Gli-animali-fanno-sorridere

if i remove the number parameter from news tag, all news are visualized

thanks for help

_________________
http://www.gianlucacompagno.com/


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays
PostPosted: Fri Oct 10, 2014 9:19 pm 
Offline
Power Poster
Power Poster

Joined: Sat Jan 22, 2005 11:19 am
Posts: 463
Quote:
I would like to view 6 articles published between all 50

I do something similar with a ListIt2 custom instance to randomly select 8 items. You will have to modify it to suit News.

Code:
    {if $items|@count > 0 && shuffle($items) !== false}   
        {$items=array_slice($items,0,8)}
            <div class="row margin-top-20 services"> 
               <!-- services -->
               {foreach from=$items item=item}
                   <!-- service -->
                     ...
                   <!-- service //-->
               {/foreach}
               <!-- services //-->
            </div>   
    {/if}       


The PHP shuffle function returns True or False so I make sure I get True returned in the {if} statement. Then I slice the array to the first 8 items.

HTH
psy


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays
PostPosted: Sat Oct 11, 2014 6:12 am 
Offline
Dev Team Member
Dev Team Member
User avatar

Joined: Wed Apr 23, 2008 7:53 am
Posts: 7690
Location: The Netherlands
Rolf wrote:
...and add counter/break code like I added in this blog https://www.cmscanbesimple.org/blog/a-x ... ain-smarty

_________________
Image

Did my post help you solving a problem at your (customers) website and it saved you many hours of work? Great!! Consider buying me a cup of coffee in return!



Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays
PostPosted: Sat Oct 11, 2014 7:06 am 
Offline
Power Poster
Power Poster

Joined: Sat Jan 22, 2005 11:19 am
Posts: 463
Quote:
...and add counter/break code like I added in this blog


Rolf, you don't need the counter or {break}.

After splicing the shuffled array, $items only contains the specified number of items, ie in the example above, a maximum of 8. The remainder are dropped.

No need for {capture} either when using the shuffle command in the {if} statement.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Show items in a random order, shuffle your Smarty arrays
PostPosted: Sat Oct 11, 2014 8:17 am 
Offline
Forum Members
Forum Members

Joined: Tue Feb 28, 2012 1:24 pm
Posts: 176
psy wrote:
Quote:
I would like to view 6 articles published between all 50

I do something similar with a ListIt2 custom instance to randomly select 8 items. You will have to modify it to suit News.

Code:
    {if $items|@count > 0 && shuffle($items) !== false}   
        {$items=array_slice($items,0,8)}
            <div class="row margin-top-20 services"> 
               <!-- services -->
               {foreach from=$items item=item}
                   <!-- service -->
                     ...
                   <!-- service //-->
               {/foreach}
               <!-- services //-->
            </div>   
    {/if}       


The PHP shuffle function returns True or False so I make sure I get True returned in the {if} statement. Then I slice the array to the first 8 items.

HTH
psy





work perfect!!!!!


{if $items|@count > 0 && shuffle($items) !== false}
{$items=array_slice($items,0,6)}

thanks thanks

_________________
http://www.gianlucacompagno.com/


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 13 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:  
cron
A2 Hosting