Add the year to your page.html.twig file

Snippet to dynamically add the year or a custom dynamic date to your page.html.twig file. In your mytheme.theme file, first add:

/**
 * Implements hook_preprocess_page().
 */
function mytheme_preprocess_page(&$variables) {
  $variables['year'] = date('Y', time());
}

Now, in your page.html.twig you can use:

{{ year }}
// Prints '2020'

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!