0

I'm using Freeform Pro to manually output a select field, but I think my question is a general Craft/Twig conditional question so I'm posting it here.

The option values in my dropdown field are made up of two variables outputted together - an entry title and a category title.

I'm trying to add a conditional for the 'selected' state of the dropdown but I can't get the conditional to work as I'm not sure of the proper syntax to for a concatenated value like this.

I've tried each of the following but none of them work:

<option value="{{ entry.title }} {{ category.entryAlternativeTitle }}" {% if requestTeam.value == 'entry.title category.title' %}selected{% endif %}>{{ entry.title }} {{ category.entryAlternativeTitle }}</option>

<option value="{{ entry.title }} {{ category.entryAlternativeTitle }}" {% if requestTeam.value == entry.title category.title %}selected{% endif %}>{{ entry.title }} {{ category.entryAlternativeTitle }}</option>

<option value="{{ entry.title }} {{ category.entryAlternativeTitle }}" {% if requestTeam.value == entry.title ~ category.title %}selected{% endif %}>{{ entry.title }} {{ category.entryAlternativeTitle }}</option>

<option value="{{ entry.title }} {{ category.entryAlternativeTitle }}" {% if requestTeam.value == {{ entry.title }} {{ category.title }} %}selected{% endif %}>{{ entry.title }} {{ category.entryAlternativeTitle }}</option>

Thanks for any help with this.

  • let us know if you were able to get this sorted out! Thanks!! – Alan Miller Aug 30 '17 at 13:25
2

It looks like according to your code this (and Aran's) solution should work except for one small difference... if you are testing for this:

entry.title ~ category.title

The value parameter (what is getting stored in the database) should match by removing the space, so from this:

{{ entry.title }} {{ category.entryAlternativeTitle }}

To this:

{{ entry.title }}{{ category.entryAlternativeTitle }}

Or you could also test for the space if you want:

{% if requestTeam.value == entry.title ~ ' ' ~ category.title %}

Update: Also want to note that you may need to test for the "entryAlternativeTitle" variable. Can't know for sure without seeing all of your code but if this is all being executed in the same chunk it should most likely be:

{% if requestTeam.value == entry.title ~ ' ' ~ category.entryAlternativeTitle %}
1

Stephen to concatenate remove the quotation marks around both titles and add the tilde between them.

{% if requestTeam.value == entry.title ~ category.title %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.