Dygne Dygne - 3 years ago 181
Twig Question

Can't access every object property in Twig

I have a

News
entity with the next properties:


  • text

  • start_date

  • expire_date



Here's part of the class:

//News.php

/**
* @ORM\Column(type="string", length=200)
*/
private $text;

/**
* @ORM\Column(type="datetime")
*/
private $start_date;

/**
* @ORM\Column(type="datetime")
*/
private $expire_date;


I want to show all the news in a list in a twig template, this is how I'm doing that:

<div class="list-group">
{%for news in news%}
<div class="panel panel-default">
<div class="panel-heading">{{ news.start_date|date('Y-m-d') }}</div>
<div class="panel-body">
{{ news.text }}
</div>
</div>
{%endfor%}
</div>


The fact is that I get the following error:

Neither the property "start_date" nor one of the methods "start_date()", "getstart_date()"/"isstart_date()" or "__call()" exist and have public access in class "AppBundle\Entity\News".


But If I try to render only the
text
property, I am able to access it.

Why is that?

Answer Source

Your attribute is private. you can add a getter

public fucntion getStartDate() {
  return $this->start_date;
}

and in twig

{{ news.startDate|date('Y-m-d') }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download