Score:0

How to display the current year in the template of a block?

cn flag

I'm trying to rewrite the "Powered by Drupal" block. I want to display the current year in my code but cale does not work. The website displays a blank page with the message "The website encountered an unexpected error. Please try again later."

This error appears when I add {{ 'now' | date('Y') }} in the code.

Here is the block template :

{% block content %}
  {{ attach_library('olivero/powered-by-block') }}
  <span>
    {% trans %}Copyright © {{ 'now' | date('Y') }} example.fr - Tous droits réservés{% endtrans %}
    <a href="https://www.example.fr">{% trans %}example.fr{% endtrans %}</a>
    <span class="example-logo" aria-label="{{ 'Example Logo'|t }}">
      <svg width="14" height="19" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 42.15 55.08" fill="none" aria-label="{{ 'Drupal Logo'|t }}" role="img">
        <path d="M29.75 11.73C25.87 7.86 22.18 4.16 21.08 0 20 4.16 16.28 7.86 12.4 11.73 6.59 17.54 0 24.12 0 34a21.08 21.08 0 1042.15 0c0-9.88-6.59-16.46-12.4-22.27zM10.84 35.92a14.13 14.13 0 00-1.65 2.62.54.54 0 01-.36.3h-.18c-.47 0-1-.92-1-.92-.14-.22-.27-.45-.4-.69l-.09-.19C5.94 34.25 7 30.28 7 30.28a17.42 17.42 0 012.52-5.41 31.53 31.53 0 012.28-3l1 1 4.72 4.82a.54.54 0 010 .72l-4.93 5.47zm10.48 13.81a7.29 7.29 0 01-5.4-12.14c1.54-1.83 3.42-3.63 5.46-6 2.42 2.58 4 4.35 5.55 6.29a3.08 3.08 0 01.32.48 7.15 7.15 0 011.3 4.12 7.23 7.23 0 01-7.23 7.25zM35 38.14a.84.84 0 01-.67.58h-.14a1.22 1.22 0 01-.68-.55 37.77 37.77 0 00-4.28-5.31l-1.93-2-6.41-6.65a54 54 0 01-3.84-3.94 1.3 1.3 0 00-.1-.15 3.84 3.84 0 01-.51-1v-.19a3.4 3.4 0 011-3c1.24-1.24 2.49-2.49 3.67-3.79 1.3 1.44 2.69 2.82 4.06 4.19a57.6 57.6 0 017.55 8.58A16 16 0 0135.65 34a14.55 14.55 0 01-.65 4.14z"/>
      </svg>
    </span>
  </span>
{% endblock %}

Log :

Error : Class "Drupal\Core\Utility\Error" not found dans _drupal_error_handler_real() (/home/clients/bj764fgg66/sites/www.example.fr/core/includes/errors.inc ligne 63)
Kevin avatar
in flag
You'd need to include the log of the error.
cn flag
@Kevin I just updated with the logs
id flag
Class "Drupal\Core\Utility\Error" is a separate, known, issue: https://www.drupal.org/project/drupal/issues/3258987 You will need a stack trace of the exception to determine the reason.
Score:2
id flag

Without the full error it is difficult to say, however, I think it is not possible to print expressions inside of {% trans %}. I think that you may be getting a Twig SyntaxError exception that reads:

The text to be translated with "trans" can only contain references to simple variables.

So, surround only the text that needs translation with {% trans %}.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.