Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig Setting Select Option on a dropdown

I am currently writing a php mvc from scratch and using twig as my template engine and need some assistance setting the selected option on a drop down list. Currently in my model I have an sql query that pull all list of supervisors and drops them in my drop down list using the twig for loop but I need to some how select a user’s supervisor if it matches up.

I apologize now as I am new to twig

View:

<select class="form-control" id="supervisor">
      {% for supervisor in supervisor %}
      <option value="{{supervisor.fname}} {{supervisor.lname}}" >{{supervisor.fname}} {{supervisor.lname}}</option>
      {% endfor %}
</select>

Tried:

<select class="form-control" id="supervisor">
      {% for supervisor in supervisor %}
{% if {{supervisor.fname}} {{supervisor.lname}} == {{ user.supervisor }} %}
            <option value=”{{supervisor.fname}} {{supervisor.lname}}” selected>{{supervisor.fname}} {{supervisor.lname}}</option>
{% else %}
    <option value=”{{supervisor.fname}} {{supervisor.lname}}”>{{supervisor.fname}} {{supervisor.lname}}</option>
{% endif %}
      {% endfor %}
</select>
like image 926
darkdelusions Avatar asked Mar 06 '17 16:03

darkdelusions


1 Answers

May be you can try something like this:
Replace supervisor variable name by oneSupervisor in the loop and test oneSupervisor with user.supervisor.

<select class="form-control" id="supervisor">
    {% for oneSupervisor in supervisor %}
        {% set selected = '' %}
        {% if (oneSupervisor.fname ~ ' ' ~ oneSupervisor.lname) == user.supervisor %}
           {% set selected = 'selected' %}
        {% endif %}
        <option value="{{oneSupervisor.fname}} {{oneSupervisor.lname}}" {{ selected }}>{{oneSupervisor.fname}} {{oneSupervisor.lname}}</option>
    {% endfor %}
</select>
like image 67
doydoy44 Avatar answered Oct 02 '22 08:10

doydoy44