Template stacks allows you to push to named stacks which can be rendered somewhere else in another snippet or template. This idea is inspired from Laravel Blade Stacks.
Calls a given stack name and prints all the items in it. The last data is printed last.
<?php stack('scripts') // directly outputted ?>
<?php echo stack('scripts', true) // returns as string ?>
Pushes data to a given stack name. The sent data is thrown to the end of the stack.
<?php push('scripts') ?>
Push any content to footer multiple times from anywhere
<?php endpush() ?>
If the pushed data is unique and in a repeating loop, you can send the secondary parameter as true
. For example; if you want a javascript library to be loaded only once in a blocks field.
<?php push('scripts', true) ?>
<script src="assets/js/plugins/slider.js"></script>
<?php endpush() ?>
/site/snippets/header.php
<html>
<body>
/site/snippets/footer.php
<?php stack('scripts') ?>
</body>
</html>
/site/templates/home.php
<?php snippet('header') ?>
<h1>Homepage</h1>
<?php push('scripts') ?>
<script src="assets/js/home.js"></script>
<?php endpush() ?>
<?php snippet('footer') ?>
/site/snippets/layouts/default.php
<html>
<body>
<?= $slots->default() ?>
<?php stack('footer') ?>
</body>
</html>
/site/templates/home.php
<?php snippet('layouts/default', slots: true) ?>
<h1>Homepage</h1>
<?php push('footer') ?>
<script src="assets/js/home.js"></script>
<?php endpush() ?>