Twig Setting Select Option on a dropdown

Twig Setting Select Option on a dropdown

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Selecting Dropdown Options in Twig Based on Model Data

Building a PHP MVC framework from scratch gives you full control over the architecture, but it also means you have to handle common web patterns like form state management manually. One of the most frequent stumbling blocks for developers new to Twig is populating a <select> element while simultaneously marking the correct option as selected based on data stored in a model or session.

The core issue often stems from a misunderstanding of how Twig renders strings versus comparing them. In your current implementation, you are iterating through a list of supervisors and constructing an option tag for each name. However, the logic inside your conditional block contains syntax errors that prevent the comparison from ever executing correctly. Specifically, the expression {% if {{supervisor.fname}} {{supervisor.lname}} == {{ user.supervisor }} %} attempts to concatenate strings directly inside the condition without using the | operator or proper variable chaining, and it tries to compare a concatenated string against a raw value without ensuring they match in format.

To fix this, you must ensure that the data being compared is identical on both sides of the equality check. If your database stores the selected supervisor as "John Doe" (first name + last name), the template logic must generate exactly that same string before comparing it to the variable holding the selected value.

Here is the corrected approach using a simple if statement within the loop:

<select class="form-control" id="supervisor">
    {% for supervisor in supervisors %}
        <option 
            value="{{ supervisor.fname }} {{ supervisor.lname }}" 
            {% if supervisor.fname ~ ' ' ~ supervisor.lname == selected_supervisor_name %}selected{% endif %}
        >
            {{ supervisor.fname }} {{ supervisor.lname }}
        </option>
    {% endfor %}
</select>

In this example, we assume selected_supervisor_name is passed to the view from your controller. The use of the ~ operator explicitly concatenates the first and last names of the current loop item to match the format of the selected value. This ensures that even if there are subtle whitespace differences or case mismatches, you can normalize the data before comparison.

A more robust pattern, often preferred in larger applications, is to compare IDs rather than full names. Comparing strings like "John Doe" vs "John Doe " (with a trailing space) can lead to bugs where no option appears selected. If your model provides an ID for the supervisor (e.g., user.supervisor_id), and your database table has a unique id column, you should pass that ID from the controller and compare integers or UUIDs instead of concatenated strings.

<select class="form-control" id="supervisor">
    {% for supervisor in supervisors %}
        <option 
            value="{{ supervisor.fname }} {{ supervisor.lname }}" 
            {% if supervisor.id == user.supervisor_id %}selected{% endif %}
        >
            {{ supervisor.fname }} {{ supervisor.lname }}
        </option>
    {% endfor %}
</select>

This approach decouples the visual representation from the state logic. If you later decide to change how names are displayed (e.g., showing full initials or just last names), your form selection logic remains intact because it relies on the stable id field.

Another consideration is performance if your list of supervisors grows large. While a simple loop works fine for typical dropdown sizes, always ensure that the variable passed to the view (user.supervisor_id) exists and is not null before attempting the comparison. Handling null values in the template can sometimes lead to unexpected rendering behavior depending on how PHP initializes the variable.

If you find yourself repeating this pattern across many forms in your custom framework, consider creating a helper method in your controller that returns an array of options where each option includes the selected flag pre-calculated. Passing this structured data to Twig simplifies the template syntax and moves the logic closer to the data source.

For developers working within the Laravel ecosystem or looking for robust examples of form handling with Twig, you might find useful references at https://laravelcompany.com, though the principles of state management in templates remain consistent regardless of the framework.

Our network