3

In Twig HTML file here is a very normal line:

{{ 'layout.logged_in_as'|trans({'%username%': app.user.username}, 'FOSUserBundle') }}

How to add a <strong></strong> tag to wrap username?

2 Answers 2

4

~ is Twig's string concatenation operator. From Twig's docs:

~: Converts all operands into strings and concatenates them.
{{ "Hello " ~ name ~ "!" }} would return Hello John! (assuming name is 'John').

 

In your case, this should translate to something like:

{{ 'layout.logged_in_as'|trans({'%username%': '<b>'~app.user.username~'</b>'}, 'FOSUserBundle') }}

 

Or perhaps:

{% set boldUsername = '<strong>' ~ app.user.username ~ '</strong>' %}
{{ 'layout.logged_in_as'|trans({'%username%': boldUsername}, 'FOSUserBundle') }}

 

If autoescaping of the html is a problem, you may need to apply the raw filter:

{% set boldUsername = '<strong>' ~ app.user.username ~ '</strong>' %}
{{ 'layout.logged_in_as'|trans({'%username%': boldUsername}, 'FOSUserBundle')|raw }}

 

Finally, if you prefer making styling in css (who doesn't), add a <span> with a class instead of <strong>:

{% set usernameMarkup = '<span class="username">' ~ app.user.username ~ '</span>' %}
{{ 'layout.logged_in_as'|trans({'%username%': usernameMarkup}, 'FOSUserBundle')|raw }}

And in a css file:

.username { font-weight: bold; }
Sign up to request clarification or add additional context in comments.

1 Comment

The following works: set boldUsername = '<strong>' ~ app.user.username ~ '</strong>' %} {{ 'layout.logged_in_as'|trans({'%username%': boldUsername}, 'FOSUserBundle')|raw }}
1
<strong>{{ 'layout.logged_in_as'|trans({'%username%': app.user.username}, 'FOSUserBundle') }}</strong>

Everything between the {{ }} twig parenthesis will be evaluated to a string, so this would be equivalent to:

<strong>Username</strong>

2 Comments

But this would make all of the 'layout.logged_in_as' message bold, not just the username, wouldn't it?
you are right, I misunderstood the question. Take a look at @Martin Lie s answer, it looks like that is what you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.