thephpleague / plates

Native PHP template system
https://platesphp.com
MIT License
1.47k stars 180 forks source link

Function Components #218

Closed ragboyjr closed 3 years ago

ragboyjr commented 6 years ago

The new v4 components feature is nice, but it still feels like a decent amount of work to get a simple component up. Also, it forces each component to have it's own file, which feels like a bit much, especially if you want some components grouped together.

I've been using internally the League\Plates\Util\obWrap function to build simple components by calling functions instead of loading the component from a file.

<?php $buildItem = function($item) {
  return League\Plates\Util\obWrap(function() use ($item) { ?>
    <li><?=$item['name']?></li>
  <?php });
}; ?>

// further down in the template

<?php foreach ($items as $item): ?>
  <?=$buildItem($item)?>
<?php endforeach; ?>

The syntax could be made more concise, but I think something like this could be like a gateway to building php templates similar to how you build React/View components.

Maybe a future syntax could be:

<?php $buildItem = $v->wrap(function($item) { ?>
  <li><?=$item['name']?></li>
<?php }); ?>

// further down in the template
<?php $v->map($buildItem, $items) ?>
ragboyjr commented 3 years ago

I'm going to be moving function components into the next release of v3 instead of bundling with v4.

But, I've been using these style components in a demo project of mine, and they are really easy to use and reason about. It's a totally different paradigm than the current templating model with plates, but I think it's better.