Twig Push/Stack Extension
So, I finally got round to writing a post on this blog after 100 years of procrastination, here we go!
Something I've always found useful in Laravel based projects was the Push/Stack directives in Blade. A simple yet powerful feature:
Define a stack anywhere in your templates (usually in your layout):
<head>
<!-- Head Contents -->
@stack('scripts')
</head>
And then "push" some markup defined in other included components/templates into this stack:
@push('scripts')
<script src="/example.js"></script>
@endpush
Surely this exists as a Twig extension somewhere? To Github!
I came across: filhocodes/twig-stack-extension, unfortunately due to massive internal changes to Twig this extension is a bit stuck in the past, so time for an upgrade!
Introducing: futureplc/twig-stack-extension
<head>
<!-- Head Contents -->
{% stack 'scripts' %}
</head>
And then push/pushonce.
{% pushonce 'scripts' %}
<script src="/path/to/your-lib.js" />
{% endpushonce %}
Feel Feel free to read through the above docs for more info. Hopefully the Twig community will find this useful!