News Category Dropdown
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.
CMS Made Simple - Modules/Add-Ons
Re: Custom JQuery Image Rotation?- Thu, 29 Jul 2010 21:23:39 GMT
I ment the CMSms Gallery module... of course Is there another module you'd prefer that makes it easier for the end user?
|
Re: Announcement: New plug-in SuperSizer- Thu, 29 Jul 2010 20:40:15 GMT
ok.. here is it.. all fixed and should be ready now to release.. |
Re: How do I get the latest version of a file- Thu, 29 Jul 2010 20:28:47 GMT
Hi |
Re: Custom JQuery Image Rotation?- Thu, 29 Jul 2010 20:21:31 GMT
wait.... i AM using the gallery module... |
Re: CGBlog - A several of things- Thu, 29 Jul 2010 20:17:22 GMTnever mind fixed it. there was some google analytics that were not liberalized. My mistake. |
CMS Made Simple Blog
Announcing CMS Made Simple 1.8.1 - Mankara- Tue, 13 Jul 2010 14:06:34 -0400This release fixes an important security vulnerability, we recommend that ALL users upgrade as soon as possible. The local inclusion vulnerability fixed is old and affects many previous versions of CMSMS. Therefore it is important for ALL... |
Announcing CMS Made Simple 1.8 - Madagascar- Sat, 03 Jul 2010 20:26:55 -0400Onwards and upwards we go. The dev team is proud to announce the latest version of your favorite content management system. This version is primarily aimed at rounding out some of the rough edges that our primary audience... |
New Site Launch- Mon, 28 Jun 2010 00:29:44 -0400After weeks of blood, sweat and tears, we've finally launched our long awaited redesign. It's taken approximately two months from design to implementation to launch, but we're finally here. Along with our main site, we've also reskinned the... |
Announcing Geek Moot 2010- Mon, 21 Jun 2010 10:19:21 -0400It's that time again. The developers of CMS Made Simple have finalized plans for our yearly user conference. This year, we're thinking much bigger, so join us. On 16-17 September, CMS Made Simple will host the second public Geek... |
Announcing CMS Made Simple 1.7.1 - Escade- Sat, 01 May 2010 09:47:13 -0400Woot! CMSMS keeps getting better and better. Even though it's only a point release, this version of CMSMS fixes numerous minor bugs and adds some important features. We didn't feel that the list of changes were extensive enough to... |


