Created
October 1, 2019 04:57
-
-
Save shmshd/669df2cead213bcc502c02ecdcb966fc to your computer and use it in GitHub Desktop.
Using .env variables in Twig using custom Twig Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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