No description
  • PHP 97.8%
  • Blade 0.8%
  • CSS 0.7%
  • JavaScript 0.7%
Find a file
2025-05-16 21:48:10 +10:00
app Initial PoC commit 2025-05-16 21:48:10 +10:00
bootstrap Initial PoC commit 2025-05-16 21:48:10 +10:00
config Initial PoC commit 2025-05-16 21:48:10 +10:00
database Initial PoC commit 2025-05-16 21:48:10 +10:00
public Initial PoC commit 2025-05-16 21:48:10 +10:00
resources Initial PoC commit 2025-05-16 21:48:10 +10:00
routes Initial PoC commit 2025-05-16 21:48:10 +10:00
storage Initial PoC commit 2025-05-16 21:48:10 +10:00
tests Initial PoC commit 2025-05-16 21:48:10 +10:00
.editorconfig Initial PoC commit 2025-05-16 21:48:10 +10:00
.env.example Initial PoC commit 2025-05-16 21:48:10 +10:00
.gitattributes Initial PoC commit 2025-05-16 21:48:10 +10:00
.gitignore Initial PoC commit 2025-05-16 21:48:10 +10:00
artisan Initial PoC commit 2025-05-16 21:48:10 +10:00
composer.json Initial PoC commit 2025-05-16 21:48:10 +10:00
composer.lock Initial PoC commit 2025-05-16 21:48:10 +10:00
LICENSE Initial PoC commit 2025-05-16 21:48:10 +10:00
package.json Initial PoC commit 2025-05-16 21:48:10 +10:00
phpunit.xml Initial PoC commit 2025-05-16 21:48:10 +10:00
README.md Initial PoC commit 2025-05-16 21:48:10 +10:00
vite.config.js Initial PoC commit 2025-05-16 21:48:10 +10:00

@formore Blade Directive PoC

A proof of concept on how a @formore directive could work and what it could look like.

How does it work?

The @formore directive works very similarly to a @forelse directive, with the exception that it requires 2 parameters in its expression:

  • The foreach definition $thingToIterate as $itemInIterable
  • The limit

The actual workings is reverse engineered from the @forelse loop.

First, a loop iteration is created with a data source, collecting the iterable into a collection for consistency. This collection is then sliced twice, once to limit the amount of items defined in the Blade expression, another for the remaining items.

Then the actual loop is started. If it's able to iterate at least once, we know the loop isn't empty, and we mark it as such.

After the loop has ended, the @more check comes into play. It checks if there are any more items available. If so, they're made available in the $more variable. $more is a Collection<int, T>.

If there are no more items, the @more directive is skipped.

If the initial iterable is empty, the @formore directive works identical to the @forelse directive, except that the @empty directive is called @formoreempty to avoid collisions.

Finally, @endformore ties everything up. It's possible to use @endforelse as well, as they both resolve to <?php endif; ?>, but that doesn't really make sense from a semantics point of view.

This PoC doesn't support nested @formore directives.

References

Example

$items = [1, 2, 3, 4, 5];
<ul>
    @formore($items as $item, 3)
    <li>Item {{ $item }}</li>
    @more
    <li>+ {{ count($more) }} more</li>
    @formoreempty
    <li>No items</li>
    @endformore
</ul>