Skip to main content.

News Category Dropdown

16/10-09 at 13.15 by: Robert Campbell

People have asked numerous times:  How do I create a pulldown of categories from the News (or some other module)... so that the user can select a category from a pulldown, and have the page automatically filter the results.

Well, I knew it would be relatively simple with some smarty magic, and a custom built (but simple) form.  So I set out to do it.... it took me all of 10 minutes.

For this demo we'll be using the News module, though the principle still applies for things like the products module, CGBlogs, or even CompanyDirectory, or if you wanted to allow the user to change the sorting.   But we'll leave that as an exercise for you to do on your own.

The first step is to create a new BrowseCategory template in the news module.  I called mine 'dropdown' and used this text:

{if $count > 0}
<form method="get">
<select name="news_category">
<option value="-1">All</option>
{foreach from=$cats item=node}
{if $node.count > 0}
<option value="{$node.news_category_id}"  {if $news_category == $node.news_category_id}selected="selected"{/if}>{$node.news_category_name}</option>
{/if}
{/foreach}
</select>
<input type="submit" name="submit" value="Submit"/>
</form>{/if}

As you can see here,  I'm creating a simple form with a dropdown and a submit button.  The dropdown has one hardcoded entry with a value of -1 and a label of "All"... the rest of the entries in the dropdown are populated dynamically from the News categories.... and only the categories with entries are added to the list.  The form uses the GET method (which means all of the parameters will be returned on the URL) but you could have just as easily used POST.  The GET method has the advantage that people will be able to bookmark the result page properly.   That's the first part of the solution.

The second part of the solution is to create a new page (or edit an existing page) and do some smarty magic on some built in smarty variables to determine what dropdown option was selected, and then call News appropriately.   Here's the code I have in my page:

{assign var='news_category'  value='-1'}
{if isset($smarty.get.news_category)}
{assign var='news_category' value=$smarty.get.news_category}
{/if}

{news action=browsecat}

<h3>Debug: News category = {$news_category}</h3>

<fieldset>
<legend>Articles:</legend>
{if $news_category != -1}
{news category_id=$news_category}
{else}
{news}
{/if}
</fieldset>

Lets start from the top.   First I use the smarty "assign" plugin to create a smarty variable called news_category with a value of -1.   Secondly, I test to see if the smarty variable $smarty.get.news_category is set, and if it is, I re-adjust the news_category smarty variable to its value.   If you look up, I gave the select box in the News browse_category template the name of news_category.

Next, I simply call {news action=browsecat}, and because I marked the dropdown browse category template as the 'default' browse category template, this will display my form.  If I didn't want to set my dropdown template as the default, I could have called news like: {news action=browsecat browsecattemplate=dropdown}.

Finally, I need to display the news articles that match the category id stored in the "news_category" smarty variable.... but if "All" is selected in the dropdown, I want to display the news articles from all categories.    I put that code inside of a fieldset with a legend so it would be easier to see.

And that's it.   Three simple steps that took me 10 minutes to implement, with no PHP coding what-so-ever.   This is the power of CMSMS and Smarty.  Once you get your head wrapped around this, the power you have in your fingers is unlimited, without really having to know how to program.

Extensions to this would be to modify the form, and page code to allow for a sort order, or maybe a page limit... and a further extension would be to get rid of the submit button (optionally) and just use some jquery triggers.  This would be trivial to do.

I hope you have fun with this.

No comments registered

Twitter / calguy1000

calguy1000: Released CGExtensions 1.27.7 for CMSMS - Minor enhancements and bug fixes.

- Fri, 03 Feb 2012 15:57:33 +0000

calguy1000: Released CGExtensions 1.27.7 for CMSMS - Minor enhancements and bug fixes.

calguy1000: RT @angst_ridden: Two great artists lost their home to a fire. Please help them recover. http://t.co/uHGcuNMN

- Thu, 02 Feb 2012 19:37:49 +0000

calguy1000: RT @angst_ridden: Two great artists lost their home to a fire. Please help them recover. http://t.co/uHGcuNMN

calguy1000: Please support the family of a CMSMS Dev Team Member. Their house was destroyed by fire yesterday: http://t.co/UfuoFWqf

- Thu, 02 Feb 2012 19:17:18 +0000

calguy1000: Please support the family of a CMSMS Dev Team Member. Their house was destroyed by fire yesterday: http://t.co/UfuoFWqf

calguy1000: CMSMS Developers! Customers in the UK? Please invite them to the GeekMoot 2012 end user workshop day - http://t.co/zarRXTHh #geekmoot #...

- Thu, 02 Feb 2012 11:36:51 +0000

calguy1000: CMSMS Developers! Customers in the UK? Please invite them to the GeekMoot 2012 end user workshop day - http://t.co/zarRXTHh #geekmoot #...

calguy1000: Edit your CMSMS site from your mobile! An exciting new app - presented at GeekMoot 2012. Book now! http://t.co/F5H3vpNH #geekmoot #cmsms

- Thu, 02 Feb 2012 10:51:54 +0000

calguy1000: Edit your CMSMS site from your mobile! An exciting new app - presented at GeekMoot 2012. Book now! http://t.co/F5H3vpNH #geekmoot #cmsms

calguy1000: Woot, today the number of members in the CMS Made Simple Linkedin group passed 500! Welcome! we enjoy the constructive dialogues :-) #cms...

- Tue, 31 Jan 2012 16:55:38 +0000

calguy1000: Woot, today the number of members in the CMS Made Simple Linkedin group passed 500! Welcome! we enjoy the constructive dialogues :-) #cms...

calguy1000: @PennyOlo for sure you should come... it's a chance to actually get your questions answered, talk to all the devs and learn how CMSMS works.

- Mon, 30 Jan 2012 19:46:47 +0000

calguy1000: @PennyOlo for sure you should come... it's a chance to actually get your questions answered, talk to all the devs and learn how CMSMS works.

calguy1000: RT @GJdeGraaf: Just used #ImageCompressor to automatically reduce all site images. Very easy way of saving data @cmsms @CMSMadeSimpleNL ...

- Sun, 29 Jan 2012 15:21:59 +0000

calguy1000: RT @GJdeGraaf: Just used #ImageCompressor to automatically reduce all site images. Very easy way of saving data @cmsms @CMSMadeSimpleNL ...

calguy1000: Playing with smarty3 and #cmsms ... not a simple conversion (smarty3 is buggy). but the caching possibilities are sweet!

- Sat, 28 Jan 2012 23:02:35 +0000

calguy1000: Playing with smarty3 and #cmsms ... not a simple conversion (smarty3 is buggy). but the caching possibilities are sweet!

calguy1000: @slabbe no sorry, it's custom ruby code... not part of CMSMS at all.

- Sat, 28 Jan 2012 07:13:07 +0000

calguy1000: @slabbe no sorry, it's custom ruby code... not part of CMSMS at all.

CMS Made Simple Blog

Geekmoot Update

- Thu, 19 Jan 2012 23:59:00 -0500

As you know our next GeekMoot will be in Northampton UK from March 8th to 10th. The Geekmoot preparations are well underway. The schedule is filling up fast, and the bookings are coming in! Here's the latest and greatest...

CMS Made Simple shares fun and page views with the fans!

- Thu, 19 Jan 2012 12:50:00 -0500

This is the second year that CMS Made Simple runs the "I Love CMSMS" fan-page promotion! The deal is quite simple. Show your community spirit by putting one of our CMSMS logos on a page of your your site with...

Nominations are Now Open for 2011 Geekmoot Awards

- Wed, 18 Jan 2012 11:40:00 -0500

Every Spring the CMS Made Simple Community recognizes the "best of" the previous year at our Geek Moot Awards Ceremony. We are now taking nominations for best of 2011 in the following categories: Developer of the Year, Design of the...

Announcing CMSMS 1.10.3 0 - Hyacynthe

- Mon, 09 Jan 2012 14:11:00 -0500

The CMSMS Dev team is proud to announce the release of CMSMS 1.10.3 - Hyacinthe. This is primarily a bug fix release on the 1.10 series, and fixes some important issues related to the wysiwyg editor and other minor inconveniences....

2011 Year In Review

- Thu, 29 Dec 2011 12:30:00 -0500

Happy Holidays, Seasons Greetings, and an early Happy New Year to all the dedicated CMSMS web professionals out there. The CMSMS dev team would like to take this time to thank you for your continued support of your favorite open...