Filtering Content with Dynamic Parameters
by Ryan Irelan
Dynamic Parameters are a documented feature of the ExpressionEngine weblog entry tag pair but it’s something you might not have used before or even known about. There are many different applications for the Dynamic Parameters functionality. In this article we’ll walk through two simple ways to use it to allow users to filter your content.
So, what exactly does it do? The Dynamic Parameter feature allows you to dynamically set any of the available weblog entries tag pair parameters (there are more than 40 to choose from). The parameters are set using the POST data from a form.
To help you master how to use Dynamic Parameters, we’re going to add the ability to customize the type of content that displays on a test version EE Insider homepage. To do this we’ll add a simple form to the top of the page that allows the user to select from the three different types of content (Blog Entries, Articles and Videos) that normally appear on the homepage.
Setting the Form
To get started we’re going to create a simple HTML form with a select element containing an option for each of the different types of content.
<form action="{path='sample/dynamic-parameters'}" method="post">
<select name="weblog" id="weblog">
<option value="blog">Blog</option>
<option value="articles">Articles</option>
<option value="videos">Videos</option>
</select>
<p><input type="submit" value="Submit"></p>
</form>
Two important things to note in the above code: the name of the select element has to be the name of the parameter you want to dynamically set and the values of the options have to be the values you want to set to the parameter.
We want the form to submit to the same page the user is viewing (in this example it’s the samples EE Insider homepage template) so we set the form action to
{path='sample/dynamic-parameters'}
which tells EE to submit the form to the sample/dynamic-parameters URI.
Getting Dynamic
Now we need to set up the existing weblog entries tag pair that populates the test homepage of the site with the dynamic_parameters parameter.
{exp:weblog:entries weblog="blog|videos|articles"
disable="trackbacks" limit="10" paginate="bottom"
dynamic_parameters="weblog"}
The value of the dynamic_parameters parameter is set to “weblog” so EE knows to dynamically set that parameter when the form data is processed. E.g. if we submit the form with “articles” selected, “articles” will be set as the value of the “weblog” parameter in our weblog entries tag pair.
How the form looks when rendered in the browser.
When submitted it will filter the content to what you selected. Don’t believe me? Give it a shot yourself on a page I set up that is a replication of the EE Insider homepage.
More than 40 Options
The cool thing about Dynamic Parameters is that you can use any of the more than 40 parameters available to the weblog entries tag pair.
Use the author_id parameter to dynamically filter the content based on the author or by category using the category parameter.
You can also dynamically set multiple parameters by separating them with a pipe.
{exp:weblog:entries weblog="blog|videos|articles"
disable="trackbacks" limit="10" paginate="bottom"
dynamic_parameters="weblog|limit"}
Using the code above, let’s add another parameter to our form, which allows us to limit the number of items displayed on the page. We simply add “limit” to the dynamic_parameters parameter and then add an additional field to our form.
<form action="{path='site/index'}" method="post">
<select name="weblog" id="weblog">
<option value="blog">Blog</option>
<option value="articles">Articles</option>
<option value="videos">Videos</option>
</select>
<select name="limit" id="limit">
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
<p><input type="submit" value="Submit"></p>
</form>
The rendered form with the limit filter option.
And now the user can select the type and number of content items that appear on the page.
Give it a try in the demo I set up and then take a shot at creating your own filters with Dynamic Parameters!
Post to Twitter
Derek Hogue — 09:04 on 05.05.2009
I recall getting very excited about this feature when I stumbled upon it in the docs, until I realized that it only accepts $_POST-ed parameters, which means the results of the filtering cannot be bookmarked by a visitor. If I go to a website and “filter by author”, I expect that I can bookmark the results page to visit again in the future and see only content by that author. Not so with this feature, as the URL always remains the same.
If this feature supported $_GET, it would be bookmark-able, and hence much more useful. I guess I should log a feature request!
AJP — 10:09 on 05.05.2009
These are GREAT when you have products, or anything you’d normally create a ton of templates for. Good call on bringing this back up!
Noah Stokes — 10:20 on 05.05.2009
Great stuff Ryan. I’ve never taken the time to learn that feature, but can see many uses for it now. Thanks for walking us through it.
Ian Cook — 10:39 on 05.05.2009
@Derek Hogue
You can create a PHP script that will accept your $_GET parameters, then redirect you (by doing a CURL POST) to the content you want. In fact, instead of redirecting, you can just include the response of the CURL request directly in that page.
Your visitors can then bookmark the $_GET version of the page.
Sean — 12:43 on 05.05.2009
I can totally see the power of this, but right now don’t have a need for it ;(
Will definitely keep this in mind for future projects.
Noel Hurtley — 13:09 on 05.05.2009
Great tutorial! Thanks so much.
John Faulds — 13:20 on 05.05.2009
Very cool, I never knew about this feature.
TRS — 16:03 on 05.05.2009
@Ian Cook - Can you elaborate on how to do this with the PHP script? Perhaps a quick tutorial?
Adam Khan — 08:44 on 05.12.2009
I’d overlooked this feature. Thanks.
Adam — 14:05 on 06.20.2009
Just curious, what if you have more than one set of exp:weblog:entries tags on the page? How do you specify which one you want the form to dynamically change?
Mark Terpstra — 03:18 on 07.22.2009
I use Dynamic Parameters all the time - but I need an easy way to paginate the results. Bookmarking the page would also be nice per the comment above. Any advise on that, Ryan?
Rob — 11:42 on 02.14.2010
Im a bit of a noob when it comes to things like this but can you substitute the dropdown for checkboxes?
Chuck Malani — 18:50 on 06.18.2010
wow - very fortunate to come across this article and this functionality. i’ve implemented it well with standard weblog entries parameters (entry_id, category_id). one case though that i cannot figure out is a date range. i’d like to filter an “events” weblog by a range of dates.
for example, i might remember that we had an event in May 2007, but i don’t remember the exact day. i’d like to have a couple of date range inputs that take “05/01/2007” and “05/31/2007” and then filters the results.
the problem i’m having is that the “event_datetime” is a custom field, not a standard parameter. and i can’t seem to use the search parameter because (a) i don’t have an exact value to search and (b) it says that text and dropdowns are searchable
any advice?
thanks,
chuck
Mike Heavers — 18:17 on 06.22.2010
Thanks for the tutorial. I posted a variation on this which utilizes EE’s search: parameter, through which you can search pretty much any field in your weblog. It’s available here:
http://mikeheavers.com/index.php/site/code_single/guided_navigation_with_expression_engine_dynamic_parameters
Hope it helps somebody, and much thanks to Ryan for helping me.
P. Colin Manikoth — 01:58 on 12.14.2010
When I thought all hope was lost, this simple solution appeared. Thanks for all your work Ryan. Here and on the EE Podcast at 5by5.tv.
Dmitry Romanovsky — 10:59 on 12.17.2010
thank you Ryan!
i need help: how do I get selected for one of the options in selected box? i’ll explain: in your example if you choose ‘videos’ then click ‘submit’ and you see the page of video content BUT with ‘blog’ selected in the ‘selected box’. Any ideas how to do that?