{title}


Code RSS Feed

Code

Guided Navigation with EE’s Dynamic Parameters

TOPICS:
  • Expression Engine

I spent two years developing on EE before I heard of its Dynamic Parameters functionality. I was tasked with creating a guided search form which allowed users to select from any number of dropdowns and input fields in order to narrow down a list of hundreds of publications to be displayed on a website. I was well familiar with EE’s somewhat lackluster search module, which allows you to search your specified weblogs/channels by keyword, but what if users don’t know what keywords they should be searching for? If you want something more guided and specific, you should make use of Dynamic Parameters. Take this example:

<form action="{path='site/results/'}" method="post">
Keywords<br/>
<
input type="text" name="search:title" />
        
Issue Area<br/>
<
select name="search:pub_issue">
    <
option>Select Issue:</option>
    
{exp:weblog:entries weblog="issues" dynamic="off"}
        
<option value="{title}">{title}</option>
    
{/exp:weblog:entries}
</select>
        
Publication Type<br/>
<
select name="search:pub_type">
    <
option></option>
    
{exp:weblog:entries weblog="pub_types" dynamic="off"}
    
<option>{title}</option>
    
{/exp:weblog:entries}
</select>
        
Year<br/>
<
input type="text" name="search:pub_date" class="pub_search_input"/>
<
input type="image" src="{site_url}images/core/button_submit.gif" value="Submit" />
</
form

The form combines a list of dropdowns and input fields, pulling content from various weblogs. The Issues dropdown pulls items from the Issues weblog, the Publication Type dropdown pulls a list of items from the pub_types weblog, and the other two fields allow a user to search for keywords within the Publication’s title and date fields, respectively. You’ll notice that each field carries a name of the dynamic parameter it will later be used to search by, for example the keywords field is given a name search:{title}. The form is set to submit to a template of our choosing. Now let’s look at that template:

{exp:weblog:entries weblog="publications" 
dynamic_parameters="search:pub_date|search:title|search:pub_issue|search:pub_type"}

<div class="pub_result {switch='|pub_alt'}">
    <
h4>{title}</h4>
    (
{pub_date}

    
<p>
        <
b>Issue: </b>{embed="embeds/pub_issue_rel" entry_id="{entry_id}"}<br/>
        <
b>Publication Type:</b{embed="embeds/pub_type_rel" entry_id="{entry_id}"}
    
</p>
    
    <
div>
        
{pub_content}
    
</div>

</
div>
{/exp:weblog:entries} 

This outputs the publications, but filters them through EE’s dynamic parameters tag, which remembers the choices made by the user in the previous page’s form. The dynamic parameters on this page consist of a pipe delimited list of the field names from the previous page (which need not be in any certain order). You’ll notice that the Issue and Publication output require an embed. This is because these fields are relationship fields which utilize Pixel and Tonic’s amazing Playa relationship fieldtype. And even though the relationships being searched technically exist within other weblogs, because we are searching only their titles, the search works in EE. Here’s one of the embeds so you can see that happening (although nested Playa relationships really require a tutorial of their own).

{exp:weblog:entries weblog="publications" entry_id="{embed:entry_id}" dynamic="off"}
  {pub_issue}
    
<span>{title}</span>
  
{/pub_issue}
{
/exp:weblog:entries} 

The embed is passed along the entry id of the matching search result from the Publications weblog, and displays the appropriate issue from the issues weblog. And that’s all there is to it. You could filter your content by every single custom field within that weblog, and EE would handle it. Note that the search is an exclusive rather than inclusive search, meaning the more search parameters you specify, the more restrictive the search will be in returning results. If all of the results do not match the search criteria, nothing will be returned.

And that’s all there is to it - in my opinion, a much better way to search (without searching) in Expression Engine.