Symfony 2细枝| date()不起作用? [英] Symfony 2 Twig |date() not working?

查看:80
本文介绍了Symfony 2细枝| date()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我真的不知道为什么.我正在使用| 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.CreatedAtNULL,所以返回的字符串基于新的日期对象:

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.

http://twig.sensiolabs.org/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 {% %}.

http://twig.sensiolabs.org/doc/templates.html#synopsis

这篇关于Symfony 2细枝| date()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆