【问题标题】:Symfony Twig - want to modify the background depending on dateSymfony Twig - 想要根据日期修改背景
【发布时间】:2020-09-29 07:32:23
【问题描述】:

我希望在天气应用程序上根据季节改变我的背景。

{% set season = data.date | date('d-m')  %}
    {% if (season >= '21-09') and (season < '21-12') %}
        <style>
        .card-weather .card-body:first-child 
        {
            background: url(https://cdn.pixabay.com/photo/2018/01/09/20/22/tree-3072431_1280.jpg) no-repeat center;
        }
        </style>

    {% elseif (season >= '21-12') and (season < '21-03') %}
        <style>
        .card-weather .card-body:first-child 
        {
            background: url(https://cdn.pixabay.com/photo/2019/01/20/11/32/eiskristalle-3943558_1280.jpg) no-repeat center;
        }
        </style>
    {% elseif (season >= '21-03') and (season <= '21-06') %}
        <style>
        .card-weather .card-body:first-child 
        {
        background: url(https://cdn.pixabay.com/photo/2017/04/12/16/22/oilseed-rape-2224941_1280.jpg) no-repeat center;
        }
        </style>
    {% elseif (season >= '22-06') and (season <= '21-09') %}
        <style>
        .card-weather .card-body:first-child 
        {
            background: url(https://cdn.pixabay.com/photo/2019/06/27/03/42/beach-4301479_1280.jpg) no-repeat center;
        }
        </style>

    {% endif %}

我将在我的控制器中查找我的日期来我的数据。 我没有看到我的错误。

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。这是一个英文网站。标题中的文字是您收到的错误消息,还是只是您忘记翻译的内容?
  • “我要在我的控制器中查找我的日期来我的数据” 无法理解。您的意思是您正在尝试在控制器中查找日期?

标签: symfony twig


【解决方案1】:

如果您真的想比较 twig 中的日期,我建议您使用 unix 格式,但您最好不要编写自定义树枝过滤器/函数来确定它是哪个季节。

{% set background = '' %}

{% set today = 'NOW'|date('U') %}
{% set spring = 'March 20'|date('U') %}
{% set summer = 'June 20'|date('U') %}
{% set fall = 'September 22'|date('U') %}
{% set winter = 'December 21'|date('U') %}

{% if today >= spring and today < summer %}
    {% set background = 'spring.png' %}
{% elseif today >= summer and today < fall %}
    {% set background = 'summer.png' %}
{% elseif today >= fall and today < winter %}
    {% set background = 'fall.png' %}
{% else %}
    {% set background = 'winter.png' %}
{% endif %}

<style>
    .card-weather .card-body:first-child {
        background: url('{{ background }}');
    }
</style>

demo - reference dates

【讨论】:

  • 这是我第一次问关于stackoverflow的问题,很高兴能回答。效果很好 非常感谢
猜你喜欢
  • 1970-01-01
  • 2022-11-28
  • 2012-02-28
  • 1970-01-01
  • 2013-12-18
  • 2022-11-16
  • 2020-02-28
  • 1970-01-01
相关资源
最近更新 更多