movie icon image

ExpressionEngine How-to Articles

Topics range from beginner to advanced, but are all born out of real world, professional, day-to-day use of ExpressionEngine.

Filtering Content with Dynamic Parameters

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!

Posted on May 05, 2009 by Ryan Irelan

Filed Under: How-To, ExpressionEngine Development

Derek Hogue14: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!

AJP15: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 Stokes15: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 Cook15: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.

Sean17: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 Hurtley18:09 on 05.05.2009

Great tutorial! Thanks so much.

John Faulds18:20 on 05.05.2009

Very cool, I never knew about this feature.

TRS21:03 on 05.05.2009

@Ian Cook - Can you elaborate on how to do this with the PHP script?  Perhaps a quick tutorial?

Adam Khan13:44 on 05.12.2009

I’d overlooked this feature. Thanks.

Adam19: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 Terpstra08: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?

Rob17: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?

Post a Comment




Please notify me of follow-up comments.


Search EE Insider
Private ExpressionEngine Training
EE Screencasts