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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 52 additions & 48 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
{
"name": "league/plates",
"description": "Plates, the native PHP template system that's fast, easy to use and easy to extend.",
"keywords": [
"league",
"package",
"templating",
"templates",
"views"
],
"homepage": "https://platesphp.com",
"license": "MIT",
"authors" : [
{
"name": "Jonathan Reinink",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "RJ Garcia",
"email": "[email protected]",
"role": "Developer"
}
],
"require" : {
"php": "^7.0|^8.0"
"name": "league/plates",
"description": "Plates, the native PHP template system that's fast, easy to use and easy to extend.",
"keywords": [
"league",
"package",
"templating",
"templates",
"views"
],
"homepage": "https://platesphp.com",
"license": "MIT",
"authors": [
{
"name": "Jonathan Reinink",
"email": "[email protected]",
"role": "Developer"
},
"require-dev": {
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
"League\\Plates\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"League\\Plates\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"scripts": {
"test": "phpunit --testdox --colors=always",
"docs": "hugo -s doc server"
{
"name": "RJ Garcia",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^7.0|^8.0",
"symfony/var-dumper": "^6.3"
},
"require-dev": {
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5",
"rector/rector": "^0.18.6",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
"League\\Plates\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"League\\Plates\\Tests\\": "tests",
"Templates\\": "exampleTemplateClass/Templates"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"scripts": {
"test": "phpunit --testdox --colors=always",
"docs": "hugo -s doc server"
}
}
35 changes: 35 additions & 0 deletions exampleTemplateClass/Templates/Layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Templates;

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

class Layout implements TemplateClassInterface
{

/**
* @param string[] $test323
*/
public function display(Template $tpl, string $company, ?string $title = null ): void { ?>
<html>
<head>
<title><?=$tpl->e($title)?> | <?=$tpl->e($company)?></title>
</head>
<body>

<?=$tpl->section('content')?>

<?=$tpl->section('scripts')?>

</body>
</html>
<?php
}
/**
* Autogenerated constructor.
*/
public function __construct(public string $company, public ?string $title = null)
{
}
}
34 changes: 34 additions & 0 deletions exampleTemplateClass/Templates/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Templates;

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

class Profile implements TemplateClassInterface
{

public function display(Template $t, callable $f, callable $e, string $name): void { ?>
<?php $t->layout(new Layout('User Profile')) ?>
<?php // $this->layout('layout', ['title' => 'User Profile']) // this is working too and will get the example/templates/layout.php ?>

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

<?php //$t->insert(new Sidebar()) ?>
<?= $f(new Sidebar()) ?>

<?php $t->push('scripts') ?>
<script>
// Some JavaScript
</script>
<?php $t->end() ?>
<?php
}
/**
* Autogenerated constructor.
*/
public function __construct(public string $name)
{
}
}
20 changes: 20 additions & 0 deletions exampleTemplateClass/Templates/Sidebar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Templates;

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

class Sidebar implements TemplateClassInterface
{

public function display(): void { ?>
<ul>
<li><a href="#link">Example sidebar link</a></li>
<li><a href="#link">Example sidebar link</a></li>
<li><a href="#link">Example sidebar link</a></li>
<li><a href="#link">Example sidebar link</a></li>
</ul>
<?php
}
}
15 changes: 15 additions & 0 deletions exampleTemplateClass/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

include '../vendor/autoload.php';

use Templates\Layout;
use Templates\Profile;

// Create new Plates instance
$templates = new League\Plates\Engine(__DIR__.'/../example/templates');

// Preassign data to the layout
$templates->addData(['company' => 'The Company Name'], Layout::class);

// Render a template
echo $templates->render(new Profile('Jonathan'));
12 changes: 12 additions & 0 deletions exampleTemplateClass/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


use League\Plates\RectorizeTemplate;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RectorizeTemplate::class);
};

// vendor/bin/rector process exampleTemplateClass/Templates --config ./exampleTemplateClass/rector.php --debug
// vendor/bin/rector process exampleTemplateClass/Templates --config ./vendor/league/plates/exampleTemplateClass/rector.php
12 changes: 8 additions & 4 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use League\Plates\Template\Name;
use League\Plates\Template\ResolveTemplatePath;
use League\Plates\Template\Template;
use League\Plates\Template\TemplateClass;
use League\Plates\Template\TemplateClassInterface;
use League\Plates\Template\Theme;

/**
Expand Down Expand Up @@ -282,20 +284,22 @@ public function exists($name)

/**
* Create a new template.
* @param string $name
* @param array $data
* @param string|TemplateClassInterface $name
* @param array $data
* @return Template
*/
public function make($name, array $data = array())
{
$template = new Template($this, $name);

$template = $name instanceof TemplateClassInterface ? new TemplateClass($this, $name)
: new Template($this, $name);
$template->data($data);
return $template;
}

/**
* Create a new template and render it.
* @param string $name
* @param string|TemplateClassInterface $name
* @param array $data
* @return string
*/
Expand Down
Loading