POC : Going from template file to template class (and get all modern PHP tools working without effort)#324
Open
RobinDev wants to merge 17 commits intothephpleague:v3from
Open
POC : Going from template file to template class (and get all modern PHP tools working without effort)#324RobinDev wants to merge 17 commits intothephpleague:v3from
RobinDev wants to merge 17 commits intothephpleague:v3from
Conversation
Author
I play with it during all the day and I conclude the best way to avoid this is to duplicate constructor and display variable to get something : <?php
namespace Templates;
use League\Plates\Template\Template;
use League\Plates\Template\TemplateClassInterface;
class Profile implements TemplateClassInterface
{
public function display(
Template $tpl,
string $name,
): void { ?>
<?php $tpl->layout(new Layout('User Profile')) ?>
<?php // $this->layout('layout', ['title' => 'User Profile']) // this is working too ! ?>
<?php // $this->layout(new Layout(), ['title' => 'User Profile']) // this is working too ! ?>
<h1>User Profile</h1>
<p>Hello, <?=$tpl->e($name)?>!</p>
<?php $tpl->insert(new Sidebar()) ?>
<?php $tpl->push('scripts') ?>
<script>
// Some JavaScript
</script>
<?php $tpl->end() ?>
<?php
}
// ---
// Autogenerated Constructor
// Prefer to restart generation instead of direct editing
public function __construct(
public string $name,
) {}
// End Autogenerated Constructor
// ---
} |
2d3c6b3 to
d38bf68
Compare
Author
|
And i finally implement it via Rector. My code is totally PHP 8, so if this proof of concept is interesting the maintainer, we can downgrade it (and test it). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello,
Context
I am heavily using plates since a month for a projet where i wanted to have static analysis and code completion than twig (or blade) could not offer in a rapid way.
I was writing template file as it (adapted from plates example folder
profile.php) :It was almost happiness when i was writing template after doing some magic on top of magic extension feature... but something was stille missing with template rendering and using layout or fetch/insert.
Proposal : It looks awful, but it's far more comfortable, and it works without BC break*
Going from the previous example, this pull request permits now to write the
profile.phpasProfile.php:Pros (and Cons)
$this->nameis totally ugly compare to$nameNext step : upgrade Plates to be fully type hinted and release a new version ?!