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

feat: Add support of Twig components extension #2094

Merged
merged 2 commits into from
Jan 17, 2025
Merged
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"intervention/image": "^3.10",
"laravel-zero/phar-updater": "^1.4",
"matthiasmullie/minify": "^1.3",
"performing/twig-components": "^0.6",
"psr/log": "^3.0",
"psr/simple-cache": "^3.0",
"scrivo/highlight.php": "^9.18",
Expand Down
63 changes: 59 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@
'extensions' => [ // list of Twig extensions class
//'<name>' => 'Cecil\Renderer\Extension\<class>',
],
'components' => [ // components
'dir' => 'components', // components directory
],
],
'themes' => [
'dir' => 'themes', // where themes are stored
Expand Down
25 changes: 24 additions & 1 deletion docs/3-Templates.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!--
description: "Working with layouts and templates."
date: 2021-05-07
updated: 2024-01-18
updated: 2025-01-17
alias: documentation/layouts
-->
# Templates
Expand Down Expand Up @@ -1226,6 +1226,29 @@ Supported values are: `short`, `medium`, `long`, and `full`.
If you want to use the `format_date` filter **with other locales than "en"**, you should [install the intl PHP extension](https://php.net/intl.setup).
:::

## Components

Cecil provides a components logic to help you build your templates.

```twig
{# /components/button.twig #}
<button {{ attributes.merge({class: 'rounded px-4'}) }}>
{{ slot }}
</button>

{# /index.twig #}
{% x:button with {class: 'text-white'} %}
<strong>Click me</strong>
{% endx %}

{# Rendered #}
<button class="text-white rounded px-4">
<strong>Click me</strong>
</button>
```

See official _Twig components extension_ documentation: <https://github.com/giorgiopogliani/twig-components#readme>.

## Built-in templates

Cecil comes with a set of [built-in templates](https://github.com/Cecilapp/Cecil/tree/master/resources/layouts).
Expand Down
8 changes: 7 additions & 1 deletion src/Renderer/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cecil\Exception\RuntimeException;
use Cecil\Renderer\Extension\Core as CoreExtension;
use Cecil\Util;
use Performing\TwigComponents\Configuration;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\IdentityTranslator;
Expand Down Expand Up @@ -122,6 +123,11 @@ public function __construct(Builder $builder, $templatesPath)

return false;
});
// components
Configuration::make($this->twig)
->setTemplatesPath($this->builder->getConfig()->get('layouts.components.dir') ?? 'components')
->useCustomTags()
->setup();
// debug
if ($this->builder->isDebug()) {
// dump()
Expand All @@ -136,7 +142,7 @@ public function __construct(Builder $builder, $templatesPath)
try {
$this->twig->addExtension(new $class($this->builder));
$this->builder->getLogger()->debug(\sprintf('Twig extension "%s" added', $name));
} catch (\Exception | \Error $e) {
} catch (RuntimeException | \Error $e) {
$this->builder->getLogger()->error(\sprintf('Unable to add Twig extension "%s": %s', $name, $e->getMessage()));
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/website/layouts/components.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'page.html.twig' %}

{% block content %}
<div>
{{ page.content}}

{% x:button with {'class': 'example-1'} %}
<strong>Click me 1</strong>
{% endx %}

<x-button class="example-2">
<em>Click me 2</em>
</x-button>
</div>
{% endblock %}
3 changes: 3 additions & 0 deletions tests/fixtures/website/layouts/components/button.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<button {{ attributes.merge({ class: 'default' }) }}>
{{ slot }}
</button>
4 changes: 4 additions & 0 deletions tests/fixtures/website/pages/Others/Components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
layout: components
---
Components examples:
Loading