twig dynamic translation domain - symfony

I'm trying to translate user content, using a translation domain depending on the role of the user.
here is my code:
{% if is_granted('ROLE_TEACHER') %}
{% set translation_domain = 'ROLE_TEACHER' %}
{% else %}
{% set translation_domain = 'ROLE_PARENT' %}
{% endif %}
<h5>{{"Main Title"|trans({}, translation_domain) }}</h5>
This works perfect for a user, in browser.
However the command for generating the translations (bin/console translation:extract en) generates them for the 'undefined' translation domain.
Is there a better way? Any ideas?
Whats the standard way for doing this? I cant seem to find any docs.

Related

Is it possible to make the FOSUserBundle "error" variable global to all twig files

I am using the FOSUserBundle in Symfony 3.4. Everything is working correctly. I am overriding the FOSUserBundle templates with my own. The way I am checking if the user failed to login because of an incorrect password or email is this way:
login.html.twig
{% trans_default_domain 'FOSUserBundle' %}
...Some html nothing fancy
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
....Rest of file content
</div>
This runs no problem. The user of course is redirected to the index page after failure handled via my security file. However, I tried using the if statement above in the index.html.twig, it thinks it is undefined. I want to use it so I can for example, when the user gets redirected to index page for my customized message to appear. Is there a for the "error" variable in that twig file to be made global to all twig files when it gets set?
The FosUserBundle stores errors in the symfony flashBag you can use this code
{% if app.request.hasPreviousSession %}
{% for type, messages in app.session.flashbag.all() %}
{% for message in messages %}
<div class="flash-{{ type }}">
{{ message }}
</div>
{% endfor %}
{% endfor %}
{% endif %}
You can look this the template of FosUserBundle https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/layout.html.twig
Or more globaly https://symfony.com/doc/current/controller.html#flash-messages
If you want to change the default errors message, you should rather replace the default translations of the FosUserBundle.

Twig: show something if user has roleX in a user list (not the current user).

In Symfony 4, I have a couple of different roles. I have a view in Twig which shows a user list. Users can have multiple roles. In the list, I want to show some text if a user has a role "MANAGER". Showing all roles is done with:
{% for role in user.roles %}
{{ role }}
{% endfor %}
Now if the user has the role "MANAGER" I want to show some text. I tried:
{% for role in user.roles %}
{% if (role is "MANAGER") %}
Show some text.
{% endif %}
{% endfor %}
but this returns the error
Unexpected token "string" of value "MANAGER" ("name" expected).
Same error is shown when I use {% if is "MANAGER") %} and when I use {% if "MANAGER") %} for some reason Show some text. is shown for every role the user has, no matter which role that is. What am I doing wrong?
As an answer to your self posted answer: a single role is not an array, the containment operator (see https://twig.symfony.com/doc/2.x/templates.html#containment-operator) supports checks for substrings as well, that's what happening here.
So you check works, but might have false-positives if you have for example a role "MINI_MANAGER", e.g.
{% set role = "MINI_MANAGER" %}
{% if "MANAGER" in role %}
Some text here.
{% endif %}
will also output "Some text here.". So the better solution would be:
{% for role in user.roles %}
{% if role == "MANAGER" %}
Some text here.
{% endif %}
{% endfor %}
This could still lead to problems when role is the boolean value "true" (that is not a Twig problem, but normal PHP behavior), so you can also have a look into the "same as" test, see https://twig.symfony.com/doc/2.x/tests/sameas.html
{% for role in user.roles %}
{% if role is same as("MANAGER") %}
Some text here.
{% endif %}
{% endfor %}
So it seems I have figured it out. It seems every single role is in fact an array, so you have to check for the value within the array like this:
{% for role in user.roles %}
{% if "MANAGER" in role %}
Some text here.
{% endif %}
{% endfor %}
I am still not sure why a single role is an array though, but there surely is a reason for that.
What about this?
{% if is_granted('ROLE_MANAGER') %}
Some text here
{% endif %}
Source: Symfony2 security functions in Twig? How to check the user's role?
See also Symfony Doc
Roles: When a user logs in, they receive a set of roles (e.g.
ROLE_ADMIN).

Using db arrays in set and include in symfony3

I'm a beginner in symfony. I love use database-arrays to fill out the pages in symfony as
<p>{{ cars.texte | nl2br }}</p>
Is it possible to use the same proceed in "set" and in "include", as
{% set tag = 'url' %}
letting the db complete the url
and
{% include "twigtemplate" %}
letting the db complete the template? This, as I have understood, doesn't work:
{% set tag = {{ cars.color }} %}
Thanks for any help!
Yes it is possible simply remove the {{ and the }}. When you are inside of the {% %} signs you don't need the {{ and }}.
{% set tag = cars.color %}

Symfony2 anon. access attribute on app.user in twig when app.user is null

When user is not logged app.user is null - i know that.
I've got a section in my twig template that is used to check for notifications.
{% if app.user.notificationsNews == 1 %}
<span class="challenge-notify animated rotateIn">!</span>
{% endif %}
no user, no notifications, no problem. Except well the user is null and it still wants to access the attribute
Impossible to access an attribute ("notificationsNews") on a null
variable
is there a good way to bypass that? |default doesn't do the trick
I could check that in my controller and return a different template file, I just want to know if there is a way to do this in twig.
You should be good to go with this
{% if app.user and app.user.notificationsNews == 1 %}
<span class="challenge-notify animated rotateIn">!</span>
{% endif %}
If you are as lazy as me, you can use the |default filter:
{% if app.user.notificationsNews|default(0) == 1 %}
<span class="challenge-notify animated rotateIn">!</span>
{% endif %}
See fiddle

Symfony2.7 Twig How to obtain a token?

Inside Twig file I have this code:
{% set player = app.security.getToken().getUser().getPlayer() %}
{% if player.getSelectedCharacter() is not null %}
{% set character = player.getSelectedCharacter() %}
{% .... %}
{% endif %}
But at now, app.security is deprecated. So I want to change this. I can obtain user token inside my controller and send it to the Twig. But I prefer to get it directly via Twig.
How I can do this?
As you said and mentioned in the documentation.
The app.security global is deprecated as of 2.6. The user is already
available as app.user and is_granted() is registered as function.
I think you can just try something like this in your view.
app.user.getPlayer()

Resources