-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 into
thephpleague:v3
Choose a base branch
from
RobinDev:template-class
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains 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
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
// ---
} |
RobinDev
force-pushed
the
template-class
branch
from
November 12, 2023 23:56
2d3c6b3
to
d38bf68
Compare
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). |
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.php
asProfile.php
:Pros (and Cons)
$this->name
is totally ugly compare to$name
Next step : upgrade Plates to be fully type hinted and release a new version ?!