Loading...


Related Products


Date Published: March 8, 2022

Accessing Acquia's Environment Variables from a Twig Template

You can expose Acquia environment variables to your Twig templates with a preprocess function using getenv():

/**
 * Implements template_preprocess_page().
 */
function MYTHEME_preprocess_page(&$variables) {
  $variables['my_variable'] = getenv('AH_SITE_ENVIRONMENT');
}

In this example, it adds the {{ my_variable }} Twig variable which contains the environment the Acquia site loads from.

Note that we are using preprocess_page() and the AH_SITE_ENVIRONMENT variable as examples. You would have to replace MYTHEME with your theme's name, and use the appropriate function and environment variable for your particular use case. See the Drupal API documentation on preprocessing for template files for more details.

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation
Back to Site navigation