shadesoft/twig-glob

This package is abandoned and no longer maintained. No replacement package was suggested.

Twig extension for usage of PHP's glob function.

v1.1 2020-02-04 23:02 UTC

This package is auto-updated.

Last update: 2023-10-02 20:19:59 UTC


README

Twig extension for usage of PHP's glob function.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require shadesoft/twig-glob

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Including into Symfony 3-4 container (if autowire and autoconfigure is set to true)

# app/config/services.yml for Symfony 3 or config/services.yaml for Symfony 4

ShadeSoft\Twig\GlobExtension: ~

Including into Symfony 2 container

# app/config/services.yml

shadesoft.twig.glob_extension:
    class: ShadeSoft\Twig\GlobExtension
    tags:
        - { name: twig.extension }

Including into Slim Framework's Twig view renderer

// src/dependencies.php

// ...
$container['view'] = function($c) {
    //...
    $view->addExtension(new ShadeSoft\Twig\GlobExtension);
    //...
}

Usage

Add \ShadeSoft\Twig\GlobExtension to your Twig environment's dependencies (or include into one of the frameworks above), then you can use the filter:

{# Without parameters, the filter will return both the matched strings and the found resource as associative array, so you can use it like below: #}
{% for size, icon in 'img/icons/favicon-*.png'|glob %}
    <link rel="icon" type="image/png" sizes="{{ size }}" href="{{ asset(icon) }}">
{% endfor %}

{# With the optional returnMatch parameter set to false, you will get a simple array with the found resources, like below: #}
{% for css in 'node_modules/@fortawesome/fontawesome-free-webfonts/css/*.css'|glob(false) %}
    <link href="{{ asset(css) }}" rel="stylesheet">
{% endfor %}