Skip to content

Instantly share code, notes, and snippets.

@shmshd
Created October 1, 2019 04:57
Show Gist options
  • Save shmshd/669df2cead213bcc502c02ecdcb966fc to your computer and use it in GitHub Desktop.
Save shmshd/669df2cead213bcc502c02ecdcb966fc to your computer and use it in GitHub Desktop.
Using .env variables in Twig using custom Twig Extension
<?php
//src/Twig/AppExtension.php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class AppExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('env', [$this, 'env']),
];
}
public function env($variable)
{
return $_ENV[$variable];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment