Published by Mijingo

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. Need more information? Get training videos and ebooks on ExpressionEngine from Mijingo.

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

Filed Under: How-To, ExpressionEngine Development,

Ryan Irelan
About Ryan Irelan

Ryan Irelan is the Technology and Development Director at Happy Cog, a web design and development firm. He is a noted ExpressionEngine expert, having created a wildly popular video training series on ExpressionEngine. Additionally, Ryan is the publisher of EE Insider, a well-known news and information site for the ExpressionEngine community. In his spare time, Ryan is the production director of A List Apart Magazine, which is one of the most popular ExpressionEngine-powered sites on the web. Recently, Ryan published a book on ExpressionEngine 2 called "ExpressionEngine 2: A Quick-Start Guide."