Skip to content
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
wants to merge 17 commits into
base: v3
Choose a base branch
from

Conversation

RobinDev
Copy link

@RobinDev RobinDev commented Nov 12, 2023

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) :

<?php
 
use App\Plates\PlatesTemplate;  // custom class extending Template with automatic extension @method generated 

/**
 * @var PlatesTemplate    $this
 * @var name            $title 
 */
?>
<?php $this->layout('layout', ['title' => 'User Profile']) ?>

<h1>User Profile</h1>
<p>Hello, <?=$this->e($name)?>!</p>

<?php $this->insert('sidebar') ?>

<?php $this->push('scripts') ?>
    <script>
        // Some JavaScript
    </script>
<?php $this->end() ?>

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 as Profile.php :

<?php

namespace Templates;

use League\Plates\Template\Template;
use League\Plates\Template\TemplateClassInterface;

class Profile implements TemplateClassInterface
{

    public function __construct(
        public string $name,
    ) {}

    public function display(Template $tpl): 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($this->name)?>!</p>

<?php $tpl->insert(new Sidebar()) ?>

<?php $tpl->push('scripts') ?>
    <script>
        // Some JavaScript
    </script>
<?php $tpl->end() ?>
<?php
    }
}

Pros (and Cons)

  • You get code completion, static code analysis and unit test can be done
  • Less magic, less headache (if magic features are deprecated)
  • Without BC breaks mean having two kinds of writing, it could be perturbing if it's not well documented. My POV is too progressively deprecate old way of doing template if you are interested by this POC
  • $this->name is totally ugly compare to $name

Next step : upgrade Plates to be fully type hinted and release a new version ?!

@RobinDev
Copy link
Author

RobinDev commented Nov 12, 2023

$this->name is totally ugly compare to $name

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
Copy link
Author

RobinDev commented Nov 13, 2023

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant