I'm new here. I'm also new in working with Symfony2.5.
I want to replace a value with another, better would be to change it via array.
It's hard to explain - here are some code parts:
Recources/Views/register.html.twig:
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{{ user.isActive }}
{% endfor %}
Here I generate the array to send to register.html.twig which looks like this:
Controller/AccountController.php
$user = $this->getDoctrine()
->getRepository('SeotoolMainBundle:User')
->findAll();
return $this->render(
'SeotoolMainBundle:Account:register.html.twig',
array('form' => $form->createView(), 'list_user' => $user)
);
It correctly outputs for isAdmin and isActive 0 or 1. Now I want to replace this output with for example isActive = 1 should Output "Active", isActive = 0 shut Output "Not active".
I hope you understand what I mean and can help me find the correct way to do this.
Thank you guys,
kind regards,
Marvin
Can't you just use an "if"?
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{% if user.isActive %}
Active
{% else %}
Inactive
{% endif %}
{% endfor %}
You can also use the ternary operator, which is more compact.
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{{ user.isActive ? 'Active' : 'Inactive' }}
{% endfor %}
Related
I use October CMS/Laravel in this case.
I have a form I send data to the next page (redirect after Submit).
On the next page I use:
function onStart(){
$this['members'] = (isset($_POST['members'])) ? $_POST['members'] : array();
}
and after that I do iterate it like this:
{% for member in members %}
{{ member }}
{% endfor %}
and I can see the result successfully like this:
{"id":2,"name":"test000","country":"","city":"test","address":"\u041f\u043e\u043f\u043e\u0432\u0430, 100","zipcode":"10998","phone":"397267786","email":"ww#ww.ww","permissions":null,"is_activated":true,"activated_at":"2022-06-29 22:23:18","last_login":"2022-07-04 09:57:09","created_at":"2022-06-29 22:23:18","updated_at":"2022-07-04 11:39:29","username":"ww#ww.ww","surname":null,"deleted_at":null,"last_seen":"2022-07-04 09:53:53","is_guest":0,"is_superuser":0,"created_ip_address":"180.183.230.180","last_ip_address":"127.0.0.1"}
{"id":3,"name":"\u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440\u0430","country":"","city":"","address":"","zipcode":"","phone":"","email":"aaa#aaa.ru","permissions":null,"is_activated":true,"activated_at":"2022-06-29 22:30:53","last_login":"2022-06-30 00:26:43","created_at":"2022-06-29 22:30:53","updated_at":"2022-06-30 00:27:07","username":"aaa#aaa.ru","surname":"","deleted_at":null,"last_seen":"2022-07-02 05:57:11","is_guest":0,"is_superuser":0,"created_ip_address":"78.25.135.90","last_ip_address":"78.25.135.90"}
but when I'm trying to parse data like this:
{% for member in members %}
{{ member.name }}
{% endfor %}
or ANY another value:
{% for member in members %}
{{ member.id }}
{% endfor %}
I see no any results.
I have two entities: Supplier and Category joined ManyToMany.
Next, I have a form builder class where I add EntityType::class.
Category structure:
id, categoryName, parentId - where parentIds value can be:
0 - head category
1 - subcategory
etc
I need to display (in twig template) categories with structure:
Category1
Subcategory1
Subcategory2
Category2
Subcategory3
Subcategory4
etc. where Category are some kind of header and subcategory are checkboxes.
Please somebody give me a tip how to do this.
Building on what we discussed in the comments, the following worked in my test environment:
InSupplierType::buildForm:
->add('categories', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name',
'group_by' => 'parent',
'multiple' => true,
'expanded' => true
])
Though without form customization this would only render the checkboxes without headers. This is discussed here: https://github.com/symfony/symfony/issues/5489#issuecomment-194943922
Implementing the fix by MaxE17677 the view would look something like this:
{% extends 'base.html.twig' %}
{% form_theme form _self %}
{# #see: https://github.com/symfony/symfony/issues/5489#issuecomment-194943922 #}
{%- block choice_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{% for name, choices in form.vars.choices %}
{% if choices is iterable %}
<label class="choice_category">
<strong>
{{ choice_translation_domain is same as(false) ? name : name|trans({}, choice_translation_domain) }}
</strong>
</label>
<div>
{% for key,choice in choices %}
{{ form_widget(form[key]) }}
{{ form_label(form[key]) }}
{% endfor %}
</div>
{% else %}
{{- form_widget(form[name]) -}}
{{- form_label(form[name], null, {translation_domain: choice_translation_domain}) -}}
{% endif %}
{% endfor %}
{%- endblock choice_widget_expanded -%}
{% block body %}
<div class="container">
{{ form_start(form) }}
{{ form_row(form.categories) }}
{{ form_end(form) }}
</div>
{% endblock %}
Preview:
The crucial thing is that you use group_by feature of the ChoiceType. To get this to work with your current entities you might need to also use the query_builder setting of EntityType. Something like:
// ...
'query_builder' => function (EntityRepository $er) {
return $er
->createQueryBuilder('c')
->where('c.parentId = 1')
;
},
along with:
// ...
'group_by' => function ($category) {
// *pseudo-code*
return $category->getParent()->getName();
}
Given an a php array in a twig template:
object(Drupal\Core\Template\Attribute)#1208 (1) {
["storage":protected]=> array(0) { }
}
How do I check if there are no non-protected elements in the array? The idea is that I can only operate on non-protected values, so I can pretend the array is empty if only protected values are present.
So far, my check is as follows:
{% if attributes is defined and attributes is not empty %}
<div{{ attributes }}>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
In its current form, this displays <div>[Content]</div>. Instead, I'd like to see: [Content]
Any help?
If this is in Drupal 8, you can pass the attributes value through render to find out, like this:
{% if attributes|render %}
<div{{ attributes }}>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
Extend twig
<?php
$twig = new Twig_Environment($loader);
$twig->addFilter(new Twig_SimpleFilter('accessible_properties', 'get_object_vars'));
Use it inside template
{% set public_attributes = attributes is defined ? (attributes|accessible_properties) : [] %}
{% if public_attributes is not empty %}
...
{% else %}
...
{% endif %}
I'm trying to create dynamically variable name so the result to be as the one below
{{ form_label(form.user_1) }}
{{ form_label(form.user_2) }}
{{ form_label(form.user_3) }}
{{ form_label(form.user_4) }}
Here's what I tried so far
{% for user in users %}
{{ form_label(form.user~'_'~loop.index) }}
{% endfor %}
but get
Argument 1 passed to
Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an
instance of Symfony\Component\Form\FormView, string given
What I'm doing wrong?
I think that you have to use the attribute() function.
If this doesn't work with the concatenation in the method parameter, try to concatenate it first in a variable like this :
{% set userIndex = 'user_' ~ loop.index %}
And then you should try this :
{{ form_label(attribute(form, userIndex)) }}
What is the shortest (and clearest) way to add comma after each element of the list except the last one?
{% for role in user.roles %}
{{ role.name }},
{% endfor %}
This example will add comma after all lines, including the last one.
Don't know about shortest but this could be clear. Try the following to add comma after all lines in the loop except the last one:
{% for role in user.roles %}
{{ role.name }}
{% if not loop.last %},{% endif %}
{% endfor %}
Shorter version as suggested in comments:
{% for role in user.roles %}
{{ role.name }}
{{ not loop.last ? ',' }}
{% endfor %}
This works with Symfony 2.3.x but should work with every 2.x version:
{{ user.roles|join(', ') }}
{{- not loop.last ? ',' : '' -}}
{{ user.roles|column('title')|join(', ') }}
Since the OP asked for iterating on name key of a role.
Shortest would be:
{{ user.roles|column('name')|join(', ') }}
where user.roles is a list of user roles.
Here is how I managed to print author names (in authors array) in some scientific publication format using twig's local loop variable in for loop-
{% spaceless %}
{% for author in authors %}
{{- loop.last ? ' and ' : (not loop.first ? ', ') -}}
{{- author -}}
{% endfor %}
{% endspaceless %}
The output is something like
For 1 author
Author1
For 2 authors
Author1 and Author2
For 3 or more authors
Author1, Author2, and Author3