Stacks
Sections are last-wins: setting one twice keeps the second. Stacks are the opposite: they append, and they aggregate across the whole render chain (the page, every nested partial, and the layout) into one place the layout reads back. This is the natural fit for "each partial contributes a script or stylesheet":
<!-- views/partials/chart.php -->
<?php push('scripts', '<script src="/chart.js"></script>'); ?>
<!-- views/pages/home.php -->
<?php push('scripts'); ?>
<script>init()</script>
<?php end_push(); ?>
<!-- views/layouts/app.php -->
<body>
<?= slot() ?>
<?= stack('scripts') ?> <!-- everything pushed to 'scripts', in order -->
</body>
Like section(), push() has a direct form (push('scripts', $html)) and a buffered push('scripts') … end_push() form. stack() returns already-rendered HTML, so, like slot(), never wrap it in e(). Stacks reset between top-level renders, so one request's contributions never leak into the next.
Because it is all just PHP, you are never fighting the tool. Any pattern you would use in plain PHP templating works here, including helper functions you define yourself.
That covers the view layer. Next we move into data, starting with the Database.