braunstetter/assets-push-bundle

A clean and easy way to extend your assets (css, js) from any point inside of your twig templates.

v0.2.1 2021-12-03 18:01 UTC

This package is auto-updated.

Last update: 2024-11-29 06:29:15 UTC


README

Scrutinizer Code Quality Build Status Total Downloads License

Push your assets from everywhere inside your templates into the <head>.

Installation

composer require braunstetter/assets-push-bundle

Usage

After the installation you can use the two tags from each template to register additional resources.

{% css '/breadcrumbs.css' %}
{% js '/breadcrumbs.js' %}

After that you are able to query the assets with the assets() function.

It will give you an array of assets like that:

[
    'css' => [
        '/breadcrumbs.css',
        '/custom.css',
        '/app.css',
    ],
    
    'js' => [
        '/breadcrumbs.js'
    ],
]

Now you can use this on top of your pages:

<head>
    <meta charset="UTF-8">
    <title>{% block title %}Welcome!{% endblock %}</title>

    {% block metadata %}{% endblock %}
    
    {% block pushedJs deferred %}
    {% for asset in assets()['js'] %}
        <script src="{{ asset(asset) }}" defer></script>
    {% endfor %}
    {% endblock %}

    {% block pushedCSS deferred %}
    {% for asset in assets()['css'] %}
        <link rel="stylesheet" href="{{ asset(asset) }}">
    {% endfor %}
    {% endblock %}
</head>