I have the following code in node.html.twig.

{{ dump(user) }}

I get this output.

object(Drupal\Core\Session\AccountProxy)#523 (4) {
object(Drupal\Core\Session\UserSession)#91 (16) {
string(1) "1"
array(2) {
  string(13) "authenticated"
  string(13) "administrator"
string(10) "1513942493"
string(5) "admin"
string(2) "en"
string(18) "noreply@imi.org.uk"
string(13) "Europe/Berlin"
string(2) "en"
string(55) "$S$E7IRVipuFHHectBpj8GkGZGPHlFWNQKG9ydDoRpwBDcCbzklpVaj"
string(1) "1"
string(10) "1513920795"

I want to render value of ["created"]=>string(10) "1513920795" and ["uid":protected]=>string(1) "1". What is the syntax or code to output them from the template?

  • All variables that can be rendered either need to be a plain value (such as a variable injected to the template via a preprocess hook) or a render array (like a field). Both are done as {{ variable }} or {{ content.variable }} respectively. The easiest way to inspect variables is to use xdebug and inspect them with a preprocess node hook in the themes .theme file, or use Twig XDebug to set a breakpoint in the twig template. – Kevin Dec 22 '17 at 16:52

You can just get the user ID and when the account was created respectively with {{ user.id }} and {{ user.created }}. With {{ user.created | date("d F Y") }} you print the timestamp with the date format you specify.

Consider user as an object. You can access all the values the same way.

See also the Twig documentation.

  • I actually want to render other variables too when i debug this way. how do i do it? if i want to render other variables like timezone, langcode etc? – Iron Butterfly Dec 22 '17 at 12:31
  • thank you for your answer. {{ dump(date) }} also gives me an output with string(..) " " I dont understand. I just want to print the raw value of the date in the front end. how do i do it? – Iron Butterfly Dec 22 '17 at 12:57
  • @IronButterfly That is a different question. It's not that because you are asking about two values taken from the user object, you will get an answer for every value a user object has. – kiamlaluno Dec 22 '17 at 13:34
  • Just use xdebug to see what your variables are, it will be less hassle. – Kevin Dec 22 '17 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.