Twig Setting Select Option on a dropdown - php

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>

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>

Assuming you have a form called form and therein a select field called field, You could simply do the following:
{% do form.field.setRendered %}
<select id="{{ form.field.vars.id }}"
name="{{ form.field.vars.name }}">
{% for option in form.field.vars.choices %}
<option {{ form.field.vars.value == option.value ? 'selected' : '' }}
value="{{ option.value }}">
{{ option.label }}
</option>
{% endfor %}
</select>

Related

TIMBER. Display posts by click the category

I display a posts using this code. Now i want to dispaly posts by clicking the category name below the grid. How can i do it?
<section class="page-section page-section--padding-2rem">
<div class="row grid-posts">
{% for post in posts %}
{% include 'partial/single-cards/recipes.twig' with {'post': post} %}
{% endfor %}
</div>
In the .php template file, add the categories to the view:
$context['categories'] = Timber::get_terms('category');
In the .twig template, render the category dropdown input.
{% if categories %}
<form action="{{ site.url }}" method="get">
<select name="cat" id="cat" onchange="return this.form.submit()">
{% for cat in categories %}
<option value="{{ cat.id }}">
{{ cat.name }}
</option>
{% endfor %}
<option value="0">Show All</option>
</select>
</form>
{% endif %}

Twig show n keys of array or all

I have an editor with a set of buttons, and I want to display only a set of buttons based on twig::render variables.
If I include all I want it to display are buttons available, if I include individual button keys I want to display only that ones.
echo TwigLoader::render('#ui/editor.html.twig'['toolbar'=>['all']]);
echo TwigLoader::render('#ui/editor.html.twig'['toolbar'=>['font','size']]);
For the template I'm using the following code:
{% set toolbar_tools = [
{'font':'<select class="ql-font"></select>'},
{'size':'<select class="ql-size"></select>'}]
%}
<div id="button-container">
<span class="ql-formats">
{% for tool, key in toolbar_tools %}
{{ tool.key|raw}}
{% endfor %}
</span>
</div>
I'm getting an empty container.
Is this a good strategy or there are better ways?
Seems you`re looking for something like this:
{% set toolbar_tools = {
'font':'<select class="ql-font"></select>',
'size':'<select class="ql-size"></select>'
}
%}
<div id="button-container">
<span class="ql-formats">
{% if toolbar|length > 0 %}
{% for t in toolbar %}
{% if t == 'all' %}
{# show all options #}
{% for tool in toolbar_tools %}
{{ tool|raw }}
{% endfor %}
{% else %}
{# show defined options #}
{{ attribute(toolbar_tools, t)|raw }}
{% endif %}
<br />
{% endfor %}
{% endif %}
</span>
</div>
Hope you will be fine with that.

Symfony Form Theming: Override country choice option template

I have a form that renders a country select like this:
<div class="form-group">
<label for="form[country]" class="its--col-sm-2 control-label">{{ form_label(form.country) }}</label>
<div class="its--col-sm-10">
{{ form_widget(form.country, { 'attr':{'id': 'countryForm', 'class': 'form-control'} }) }}
</div>
</div>
The problem comes with AOTranslationBundle.
It's a great bundle. Registers in db all translations that are being rendered in your view and show them in the toolbar for translation.
Then, symfony's country choice tries to translate all countries that Symfony\Component\Intl\Intl::getRegionBundle()->getCountryNames() returns.
I solved this overriding choice_widget_options
{% form_theme form _self %}
{%- block choice_widget_options -%}
{% for group_label, choice in options %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label }}</option>
{% endfor %}
{%- endblock choice_widget_options -%}
My question is... How can i override just country select options and not other select options?
You need to override for that particular field only.
You can try this approach.
Customize individual field

Symfony 2 form theme with choice field

I have problem. I want to do choices country with flag ico but I don't know how to create custom theme for form choice filed.
I created test form :
->add('name', 'choice', array('choices' =>array('en' => 'England', 'de' => 'Deutshland')))
Next in my view I try
{% block _send_name_widget %}
<select>
{% for f in form %}
{{ loop.index }}
{%endfor%}
</select>
{% endblock%}
{{ form_widget(form.name) }}
And in my html code I've got:
<select>
1
2
</select>
<select>
</select>
Could you tell me why?
How can I render only one select with parameters?
The variable "options" didn't exist in my choice_widget_options template. The fact is that "options" IS NOT the correct name of the variable. If you take a look at \vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\views\Form\choice_widget_options.html.php, you'll find Symfony use a variable called 'choices'.
The corrected version of the previous code is :
{% block choice_widget_options %}
{% spaceless %}
{% for group_label, choice in choices %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>
<img src="/images/flags/{{ choice.label }}.jpg" />
{{ choice.label|trans({}, translation_domain) }}
</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}
Override the choice template:
{% block choice_widget_options %}
{% spaceless %}
{% for group_label, choice in choices %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>
<img src="/images/flags/{{ choice.label }}.jpg" />
{{ choice.label|trans({}, translation_domain) }}
</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}
more info about form theming in the Symfony2 docs

How to get form variables in custom form field in symfony2

I have this custom form field
{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
{% block gender_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}
This renders the checkboxes. But i am not able to find how can i get the value of checkbox
i.e child.form.value is not working.
Suppose i have entities which is named as tasks in the form.
how can i get the value of the taskid.
something like
child.form.vars.task.id
It seems to be in {{ choice.value }}
Have a look at this to see how the inheritance works.
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>

Categories