0

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

{{ dump(user) }}

I get this output.

object(Drupal\Core\Session\AccountProxy)#523 (4) {
["account":protected]=>
object(Drupal\Core\Session\UserSession)#91 (16) {
["uid":protected]=>
string(1) "1"
["roles":protected]=>
array(2) {
  [0]=>
  string(13) "authenticated"
  [1]=>
  string(13) "administrator"
}
["access":protected]=>
string(10) "1513942493"
["name"]=>
string(5) "admin"
["preferred_langcode":protected]=>
string(2) "en"
["preferred_admin_langcode":protected]=>
NULL
["mail":protected]=>
string(18) "noreply@imi.org.uk"
["timezone":protected]=>
string(13) "Europe/Berlin"
["langcode"]=>
string(2) "en"
["pass"]=>
string(55) "$S$E7IRVipuFHHectBpj8GkGZGPHlFWNQKG9ydDoRpwBDcCbzklpVaj"
["status"]=>
string(1) "1"
["created"]=>
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
1

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.