我有一个小问题,我真的不知道为什么.我正在使用| date()在细枝中打印日期时间变量,但始终打印实际时间.
i have a little problem and I really don't know why. I am printing a datetime variable in twig with |date() but it is allways printing the actual time.
为了进行调试,我在模板中添加了以下代码:
for Debugging I put the following Code in my Template:
<pre> {% debug entity.getCreatedAt|date("d.m.Y H:i:s") %} {% debug entity.getCreatedAt|raw %} {% debug entity.CreatedAt|raw %} {% debug entity.CreatedAt|date("d.m.Y H:i:s") %}
我的变量称为CreatedAt,因此通常我应该使用entity.CreatedAt | date("d.m.Y H:i:s")获得正确的输出,对吗?
My Variable is called CreatedAt so normally I should get the correct output with entity.CreatedAt|date("d.m.Y H:i:s"), right?
我的Debug输出如下:
My Debug output is as follows:
string '16.01.2013 13:46:03' (length=19) //entity.getCreatedAt|date("d.m.Y H:i:s") object(DateTime)[4611] //entity.getCreatedAt|raw public 'date' => string '2013-01-16 13:46:03' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'Europe/Berlin' (length=13) object(DateTime)[4938] //entity.CreatedAt|raw public 'date' => string '2013-02-20 21:46:53' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'Europe/Berlin' (length=13) string '20.02.2013 21:46:53' (length=19) //entity.CreatedAt|date("d.m.Y H:i:s")当我调用CreatedAt时,我不明白为什么它为NULL.在调试标记之外,它始终为NULL,而不取决于编写内容.
I don't understand why it is NULL as soon as I call CreatedAt. And OUTSIDE of the debug tag it is ALLWAYS NULL, not depending on the writing.
在我的实体中,我有:
private $CreatedAt; public function setCreatedAt($createdAt) { $this->CreatedAt = $createdAt; return $this; } public function getCreatedAt() { return $this->CreatedAt; }在YML中,我得到了:
And in the YML I've got:
CreatedAt: type: datetime nullable: true有人看到错误了吗?我真的找不到它,也许是个错误?
Does anybody see a mistake?? I really don't find it, maybe it is a bug?
谢谢
推荐答案如果您想在实体类之外访问您的 private 变量$CreatedAt,则必须调用 public 吸气剂方法getCreatedAt()(这就是这里的用途).
If you want to access your private variable $CreatedAt outside your entity class, you have to call the public getter method getCreatedAt() (that's what it's here for).
在树枝模板中,调用{% debug entity.CreatedAt|date("d.m.Y H:i:s") %}时,由于entity.CreatedAt是NULL,所以返回的字符串基于新的日期对象:
And in your twig template, when you call {% debug entity.CreatedAt|date("d.m.Y H:i:s") %}, since entity.CreatedAt is NULL, the returned string is based on a new date object:
如果传递给日期过滤器的值为null,则默认情况下它将返回当前日期.
If the value passed to the date filter is null, it will return the current date by default.
twig.sensiolabs/doc/filters/date.html
更新:
如@insertusername此处所述,twig自动调用公共获取程序. 但是,仅当在{% %}上使用定界符{{ }}时,才会出现这种情况.
As mentioned by @insertusernamehere, twig automatically calls the public getter. But this behavior seems to happen only when using the delimiters {{ }} over {% %}.
twig.sensiolabs/doc/templates.html#synopsis
更多推荐
Symfony 2细枝
发布评论