Skip to content

Instantly share code, notes, and snippets.

@zburgermeiszter
Last active August 9, 2022 19:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zburgermeiszter/afb9cb14b9a6f3f023e1 to your computer and use it in GitHub Desktop.
Save zburgermeiszter/afb9cb14b9a6f3f023e1 to your computer and use it in GitHub Desktop.
TWIG Base64 extension for Symfony
<?php
namespace Vendor\Bundle\Twig;
/*
services.yml:
twig.base64:
class: Vendor\Bundle\Twig\Base64
tags :
- { name: twig.extension }
*/
class Base64 extends \Twig_Extension {
public function getFilters(){
return array(
new \Twig_SimpleFilter('base64_encode', 'base64_encode'),
new \Twig_SimpleFilter('base64_decode', 'base64_decode')
);
}
public function getName(){
return "base64";
}
}
@j0k3r
Copy link

j0k3r commented Jan 19, 2022

With more recents versions of Twig:

<?php

namespace Vendor\Bundle\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class Base64 extends AbstractExtension
{
    public function getFilters()
    {
        return [
            new TwigFilter('base64_encode', 'base64_encode'),
            new TwigFilter('base64_decode', 'base64_decode'),
        ];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment