<p>I'd <a href="http://twig.sensiolabs.org/doc/filters/split.html" rel="nofollow">split the parameter string</a> into an array and then loop through the categories and use a conditional to check if the value already exists in the parameter. It's not so easy to describe in detail, that's why I added lots of comments to the code (untested!).</p>

<p>To work in your other parameters (size, etc.), simply add them to your links exactly how <a href="http://buildwithcraft.com/docs/templating/craft.request#getParam" rel="nofollow"><code>craft.request.getParam()</code></a> returns them. Conditionally skip the output completely if the param is empty.</p>

<pre class="lang-none prettyprint-override"><code>{# Get the parameter (e.g. 'red,blue,black') #}
{% set currentParam = craft.request.getParam('color') %}

{# Split the parameter string #}
{% set arrCurrentParam = currentParam|split(',') %}

{# Loop through categories to output the links #}
{% for catOut in categories %}

    {# Define newParam var before loop #}
    {% set newParam = '' %}

    {# Loop through categories to compose the new parameter #}
    {% for catComp in categories %}

        {# Check if changes could happen in this loop #}
        {% if catOut.slug == catComp.slug %}

            {# Add category if it was currently NOT selected #}
            {% if catComp.slug not in arrCurrentParam %}
                {% set newParam = newParam ~ ',' ~ catComp.slug %}
            {% endif %}

        {% else %}

            {# Add category if it was already selected #}
            {% if catComp.slug in arrCurrentParam %}
                {% set newParam = newParam ~ ',' ~ catComp.slug %}
            {% endif %}

        {% endif %}

    {% endfor %}

    {# Remove leading comma #}
    {% set newParam = newParam|slice(1) %}

    {# Conditionally add param to link output #}
    &lt;a href="product?{{ newParam ? 'color=' ~ newParam }}"&gt;{{ category.slug }}&lt;/a&gt;

{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-08-26T13:18:53.340Z
you made my day!!!!
Johannes Lamers
CC BY-SA 3.0
2014-08-26T13:29:22.140Z
Glad I could help, @Johannes. It was a challenge to think that one through though! But yeah, I'm sure I need something similar sooner or later, so it was definitely worth it :)
carlcs
CC BY-SA 3.0
2014-08-26T13:33:53.720Z
I think more crafters need this too. Thank you again!
Johannes Lamers
CC BY-SA 3.0
2014-08-26T12:26:20.930Z
2014-08-26T13:00:59.663Z
2014-08-26T13:00:59.663Z
carlcs
<p>How do you update an URL with new params?</p>

<p>I'm working on a search / filter entries page. I have multiple menu lists with categories in it. Like this:</p>

<p><strong>Size:</strong>
Small
Medium
Large</p>

<p><strong>Color:</strong>
Blue
Red
White
Yellow</p>

<p>So if the URL is <code>product?size=m</code> and I click on a link "Size → Large" then the URL needs to update to <code>product?size=m,l</code>. Click again on "Large" the <code>l</code> value of the sizes parameter needs to delete.</p>

<p>If I then click on "Color → Red" the URL must be <code>product?size=m,l&amp;color=red</code>.</p>

CC BY-SA 3.0
2014-08-26T10:14:56.920Z
So you actually want to know how to compose the parameters and not how you "update an URL".
carlcs
CC BY-SA 3.0
2014-08-26T10:17:19.743Z
:-) yes. Sorry, my english isn't good
Johannes Lamers
<templating><url><request><category><post>
Compose URL POST parameters to filter entries by category?
# Compose URL POST parameters to filter entries by category?

(Asked by: username_0 on 2014-08-26)

How do you update an URL with new params?

I'm working on a search / filter entries page. I have multiple menu lists with categories in it. Like this:

**Size:**
Small
Medium
Large

**Color:**
Blue
Red
White
Yellow

So if the URL is `product?size=m` and I click on a link "Size → Large" then the URL needs to update to `product?size=m,l`. Click again on "Large" the `l` value of the sizes parameter needs to delete.

If I then click on "Color → Red" the URL must be `product?size=m,l&color=red`.

**Tags:** templating, url, request, category, post

> **Comments:**
> **username_1**: So you actually want to know how to compose the parameters and not how you "update an URL".  
> **username_0**: :-) yes. Sorry, my english isn't good  

---

## 1 answers

### Answer by username_1 on 2014-08-26. Score: 4 | Accepted answer

I'd split the parameter string into an array and then loop through the categories and use a conditional to check if the value already exists in the parameter. It's not so easy to describe in detail, that's why I added lots of comments to the code (untested!).
To work in your other parameters (size, etc.), simply add them to your links exactly how craft.request.getParam() returns them. Conditionally skip the output completely if the param is empty.
{# Get the parameter (e.g. 'red,blue,black') #}
{% set currentParam = craft.request.getParam('color') %}

{# Split the parameter string #}
{% set arrCurrentParam = currentParam|split(',') %}

{# Loop through categories to output the links #}
{% for catOut in categories %}

    {# Define newParam var before loop #}
    {% set newParam = '' %}

    {# Loop through categories to compose the new parameter #}
    {% for catComp in categories %}

        {# Check if changes could happen in this loop #}
        {% if catOut.slug == catComp.slug %}

            {# Add category if it was currently NOT selected #}
            {% if catComp.slug not in arrCurrentParam %}
                {% set newParam = newParam ~ ',' ~ catComp.slug %}
            {% endif %}

        {% else %}

            {# Add category if it was already selected #}
            {% if catComp.slug in arrCurrentParam %}
                {% set newParam = newParam ~ ',' ~ catComp.slug %}
            {% endif %}

        {% endif %}

    {% endfor %}

    {# Remove leading comma #}
    {% set newParam = newParam|slice(1) %}

    {# Conditionally add param to link output #}
    {{ category.slug }}

{% endfor %}

> **Comments:**
> **username_0**: you made my day!!!!  
> **username_1**: Glad I could help, @Johannes. It was a challenge to think that one through though! But yeah, I'm sure I need something similar sooner or later, so it was definitely worth it :)  
> **username_0**: I think more crafters need this too. Thank you again!  

---

