Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Twig Ext Bundle Laravel Package

ano/twig-ext-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require ano/twig-ext-bundle
    

    Enable the bundle in config/bundles.php (Symfony 4+):

    return [
        // ...
        Ano\TwigExtBundle\AnoTwigExtBundle::class => ['all' => true],
    ];
    
  2. First Use Case Anonymize sensitive data in Twig templates (e.g., user profiles, logs). Example:

    {{ user.email|anonymize }}
    

    Output: user****@example.com (if configured to mask emails).


Implementation Patterns

Common Workflows

  1. Data Masking in Templates Use filters for anonymization:

    {# Mask full names #}
    {{ user.full_name|anonymize }}
    
    {# Mask phone numbers #}
    {{ user.phone|anonymize(3) }} {# Keep last 3 digits #}
    
  2. Dynamic Anonymization Logic Extend the bundle’s AnoTwigExtExtension to add custom rules:

    // src/Twig/AppExtension.php
    use Ano\TwigExtBundle\Twig\AnoTwigExtExtension;
    
    class AppExtension extends AnoTwigExtExtension {
        public function getFunctions() {
            return [
                new \Twig\TwigFunction('custom_anonymize', [$this, 'customAnonymize']),
            ];
        }
    
        public function customAnonymize($value, $maskLength = 4) {
            return substr($value, 0, -$maskLength) . str_repeat('*', $maskLength);
        }
    }
    

    Register in services.yaml:

    services:
        App\Twig\AppExtension:
            tags: ['twig.extension']
    
  3. Integration with Symfony Forms Anonymize form data before display:

    <td>{{ form_row(form.email)|replace({'value': value|anonymize }) }}</td>
    

Gotchas and Tips

Pitfalls

  1. Twig Version Mismatch The package requires twig/twig:1.*. If using Symfony 5+, pin the version in composer.json:

    "require": {
        "twig/twig": "1.44.*"
    }
    
  2. Bundle Not Auto-Discovered Ensure AnoTwigExtBundle is listed in config/bundles.php. For Symfony <4, add to AppKernel.php:

    new Ano\TwigExtBundle\AnoTwigExtBundle(),
    
  3. Overriding Default Filters The anonymize filter uses a simple **** mask. Override globally in config/packages/twig.yaml:

    twig:
        globals:
            twig_ext_anonymize_mask: '---'
    

Debugging Tips

  • Check Registered Extensions Dump Twig extensions to verify registration:

    // In a controller
    $twig = $this->get('twig');
    dump($twig->getExtension('AnoTwigExtExtension'));
    
  • Custom Filter Not Working? Ensure your extension class implements Twig\Extension\ExtensionInterface and is tagged correctly.

Extension Points

  1. Add New Anonymization Rules Extend AnoTwigExtExtension and override getFilters():

    public function getFilters() {
        return [
            new \Twig\TwigFilter('anonymize_ip', [$this, 'anonymizeIp']),
            // ... existing filters
        ];
    }
    
  2. Conditional Anonymization Use Twig tests to conditionally apply filters:

    {% if is_sensitive_data(user.email) %}
        {{ user.email|anonymize }}
    {% else %}
        {{ user.email }}
    {% endif %}
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver