Skip to content

Commit

Permalink
Add Spiral Twig Integration (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Nov 9, 2022
1 parent ac5c214 commit 6ab7994
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ they are supporting. Also, what kind of features are supported by them.
| Engine | Version | PHP Version | Inheritance | Subviews | Namespaces | Functions | Filters | Exist | Partial | Streaming | String | Raw | Globals |
|--------------|----------|------------------------|-------------|----------|------------|-----------|---------|-------|---------|-----------|--------|-----|---------|
| Twig | `3.4.1` | `>=7.2.5` | [x] | | [x] `@` | [x] | [x] | [x] | [x] | [x] | [x] | [x] | [x] |
| Blade | `9.15.0` | `^8.1` | [x] | | [x] `::` | ? | ? | ? | ? | ? | [x] | ? | ? |
| Blade | `9.15.0` | `^8.1` | [x] | | [x] `::` | ? | ? | ? | [x] | ? | [x] | ? | ? |
| Latte | `3.0.0` | `>=8.0 <8.2` | [x] | | ? | | [x] | ? | ? | ? | [x] | [x] | ? |
| | | | | | | | | | | | | | |
| Plates | `3.4.0` | `^7.0 ^8.0` | [x] | | [x] `::` | [x] | | ? | | ? | [x] | ? | ? |
Expand Down Expand Up @@ -493,7 +493,8 @@ E.g.: This is achieved in twig via twig extension, twig filters and the filter `
The template engine allows to render only a subpart of a template.

E.g. This is achieved in twig via loading the template and use the
templates `renderBlock` method.
templates `renderBlock` method. In blade via the fragment method on
the view newly added in [lately](https://twitter.com/taylorotwell/status/1590066457458913280).

#### Streaming

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@

namespace App\Api\Web\Controller;

use Schranz\Templating\Adapter\Twig\TwigRenderer;
use Schranz\Templating\TemplateRenderer\TemplateRendererInterface;
use Spiral\Router\Annotation\Route;

class TemplateController
{
public function __construct(
private ?TemplateRendererInterface $defaultRenderer
) {
}

#[Route(route: '/', name: 'index', methods: 'GET')]
public function index(): string
public function index(?TemplateRendererInterface $defaultRenderer = null): string
{
return
'<h1>Goto /</h1>' .
'<p>Default Renderer is: ' . get_debug_type($this->defaultRenderer) . '</p>'
'<h1>Goto /twig</h1>' .
'<p>Default Renderer is: ' . get_debug_type($defaultRenderer) . '</p>'
;
}

#[Route(route: '/twig', name: 'twig', methods: 'GET')]
public function twigRenderer(TwigRenderer $twigRenderer): string
{
return $twigRenderer->render(
'base.twig',
[
'title' => 'Render using: ' . get_class($twigRenderer),
]
);
}
}
4 changes: 4 additions & 0 deletions examples/usages/spiral/app/src/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class Kernel extends \Spiral\Framework\Kernel
RoadRunnerBridge\CommandBootloader::class,

Bootloader\RoutesBootloader::class,

// Custom
\Spiral\Twig\Bootloader\TwigBootloader::class,
\Schranz\Templating\Integration\Spiral\Twig\Bootloader\TwigBootloader::class,
];

/*
Expand Down
17 changes: 17 additions & 0 deletions examples/usages/spiral/app/views/base.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{% block title %}
Welcome!
{% endblock %}
</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text></svg>">
</head>
<body>
{% block content %}
<h1>{{ title }}</h1>
{% endblock %}
</body>
</html>
26 changes: 25 additions & 1 deletion examples/usages/spiral/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@
"issues": "https://github.com/spiral/app/issues",
"source": "https://github.com/spiral/app"
},
"repositories": [
{
"type": "path",
"url": "./../../../src/TemplateRenderer",
"options": {
"symlink": true
}
},
{
"type": "path",
"url": "./../../../src/Adapter/Twig",
"options": {
"symlink": true
}
},
{
"type": "path",
"url": "./../../../src/Integration/Spiral/Twig",
"options": {
"symlink": true
}
}
],
"require": {
"php": ">=8.1",
"ext-mbstring": "*",
Expand All @@ -16,7 +39,8 @@
"spiral/framework": "^3.2",
"spiral/nyholm-bridge": "^1.3",
"spiral/roadrunner-bridge": "^2.1",
"spiral/sapi-bridge": "^1.0.1"
"spiral/sapi-bridge": "^1.0.1",
"schranz-templating/spiral-twig-integration": "@dev"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
2 changes: 2 additions & 0 deletions src/Integration/Spiral/Twig/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
54 changes: 54 additions & 0 deletions src/Integration/Spiral/Twig/Bootloader/TwigBootloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Schranz\Templating\Integration\Spiral\Twig\Bootloader;

use Schranz\Templating\Adapter\Twig\TwigRenderer;
use Schranz\Templating\TemplateRenderer\TemplateRendererInterface;
use Spiral\Core\Container;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Twig\TwigEngine;
use Spiral\Views\ViewContext;
use Spiral\Views\ViewManager;
use Twig\Environment;

class TwigBootloader extends Bootloader
{
public function boot(Container $container): void
{
$container->bindSingleton('twig', function (Container $container) {
// Use a hack to get Twig from the ViewManager as it is not available as an service yet
$viewManager = $container->get(ViewManager::class);

$engines = $viewManager->getEngines();

$twigEngine = null;
foreach ($engines as $engine) {
if ($engine instanceof TwigEngine) {
$twigEngine = $engine;
break;
}
}

if (null === $twigEngine) {
throw new \LogicException(
\sprintf('Expected "%s" to be registered in the view manager.', TwigEngine::class)
);
}

return $twigEngine->getEnvironment(new ViewContext());
});

$container->bind(Environment::class, 'twig');

$container->bindSingleton('schranz_templating.renderer.twig', function (Container $container) {
return new TwigRenderer($container->get('twig'));
});

$container->bind(TemplateRendererInterface::class, 'schranz_templating.renderer.twig');
$container->bind(TwigRenderer::class, 'schranz_templating.renderer.twig');

$container->bindSingleton('schranz_templating.renderer.twig', function (Container $container) {
return new TwigRenderer($container->get('twig'));
});
}
}
21 changes: 21 additions & 0 deletions src/Integration/Spiral/Twig/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Alexander Schranz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions src/Integration/Spiral/Twig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Schranz Template Renderer Integration for Twig

Integrate the templating [Twig Adapter](https://github.com/schranz-templating/twig-adapter)
into the Spiral Framework.

Part of the [Schranz Templating Project](https://github.com/schranz-templating/templating).

## Installation

Install this package via Composer:

```bash
composer require schranz-templating/spiral-twig-integration
```

Register the Bootloader class in your `app/src/Application/Kernel.php` if not already automatically
added by the framework:

```php
class Kernel extends \Spiral\Framework\Kernel
{
protected const LOAD = [
// ...
\Spiral\Twig\Bootloader\TwigBootloader::class,
\Schranz\Templating\Integration\Spiral\Twig\Bootloader\TwigBootloader::class,
];
}
```

## Configuration

The Twig Integration has currently no configuration as Twig
is supported out of the box by Spiral and can be configured
via the [Spiral Twig Views](https://spiral.dev/docs/views-twig).
39 changes: 39 additions & 0 deletions src/Integration/Spiral/Twig/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "schranz-templating/spiral-twig-integration",
"description": "A integration of template renderer into spiral via twig template engine.",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Schranz\\Templating\\Integration\\Spiral\\Twig\\": ""
}
},
"repositories": [
{
"type": "path",
"url": "./../../../TemplateRenderer",
"options": {
"symlink": true
}
},
{
"type": "path",
"url": "./../../../Adapter/Twig",
"options": {
"symlink": true
}
}
],
"require": {
"php": "^7.2 || ^8.0",
"schranz-templating/twig-adapter": "^0.1",
"spiral/twig-bridge": "^1.0 || ^2.0",
"psr/container": "^1.0 || ^2.0"
},
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"spiral/composer-publish-plugin": true
}
}
}

0 comments on commit 6ab7994

Please sign in to comment.