Skip to content

Commit 1a18875

Browse files
authored
Rewrite of the readme (#30)
* Rewrite of the readme Attempt to clarify the readme :-) * Update README.md * Update README.md * Update README.md
1 parent be0f419 commit 1a18875

File tree

1 file changed

+109
-109
lines changed

1 file changed

+109
-109
lines changed

README.md

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Twig components extension
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/performing/twig-components.svg?style=flat-square)](https://packagist.org/packages/performing/twig-components)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/giorgiopogliani/twig-components/Tests)](https://github.com/giorgiopogliani/twig-components/actions?query=workflow%3ATests+branch%3Amaster)
5-
[![Total Downloads](https://img.shields.io/packagist/dt/performing/twig-components.svg?style=flat-square)](https://packagist.org/packages/performing/twig-components)
3+
[![Packagist Version](https://img.shields.io/packagist/v/performing/twig-components)](https://packagist.org/packages/performing/twig-components) [![Tests](https://github.com/giorgiopogliani/twig-components/actions/workflows/run-tests.yml/badge.svg)](https://github.com/giorgiopogliani/twig-components/actions/workflows/run-tests.yml) [![Packagist Downloads](https://img.shields.io/packagist/dt/performing/twig-components)](https://packagist.org/packages/performing/twig-components)
64

7-
This is a PHP package for automatically create Twig components as tags. This is highly inspired from Laravel Blade Components.
5+
This is a PHP package for automatically create Twig components as tags. This is highly inspired from Laravel Blade Components.
86

97
## Installation
108

@@ -19,22 +17,20 @@ composer require performing/twig-components
1917
This package should work anywhere where Twig is available.
2018

2119
```php
22-
/** @var \Twig\Environment $twig */
23-
2420
use Performing\TwigComponents\Configuration;
2521

22+
/** @var \Twig\Environment $twig */
2623
Configuration::make($twig)
27-
->setTemplatesPath('/relative/directory/to/components')
28-
->setTemplatesExtension('twig')
29-
->useCustomTags()
24+
->setTemplatesPath('/relative/directory/to/components') // default is 'components'
25+
->setTemplatesExtension('twig') // default is 'twig'
3026
->setup();
3127
```
3228

3329
To enable the package just pass your Twig environment object to the function and specify your components folder relative to your Twig templates folder.
3430

35-
### Craft CMS
31+
### Configuration for Craft CMS
3632

37-
In Craft CMS you should do something like this.
33+
In Craft CMS you should do something like this:
3834

3935
```php
4036
// Module.php
@@ -44,33 +40,28 @@ if (Craft::$app->request->getIsSiteRequest()) {
4440
Plugins::EVENT_AFTER_LOAD_PLUGINS,
4541
function (Event $event) {
4642
$twig = Craft::$app->getView()->getTwig();
47-
\Performing\TwigComponents\Configuration::make($twig)
48-
->setTemplatesPath('/components')
49-
->useCustomTags()
50-
->setup();
43+
\Performing\TwigComponents\Configuration::make($twig)->setup();
5144
}
5245
);
5346
}
5447
```
5548

5649
> The `if` statement ensure you don't get `'Unable to register extension "..." as extensions have already been initialized'` as error.
5750
58-
### Symfony
51+
### Configuration for Symfony
5952

60-
In Symfony you can do something like this.
53+
In Symfony you can do something like this:
6154

6255
```yml
6356
# services.yml
64-
6557
services:
6658
My\Namespace\TwigEnvironmentConfigurator:
6759
decorates: 'twig.configurator.environment'
68-
arguments: [ '@My\Namespace\TwigEnvironmentConfigurator.inner' ]
60+
arguments: ['@My\Namespace\TwigEnvironmentConfigurator.inner']
6961
```
70-
```php
7162
63+
```php
7264
// TwigEnvironmentConfigurator.php
73-
7465
use Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator;
7566
use Twig\Environment;
7667
use Performing\TwigComponents\Configuration;
@@ -84,60 +75,45 @@ final class TwigEnvironmentConfigurator
8475
public function configure(Environment $environment) : void
8576
{
8677
$this->decorated->configure($environment);
87-
88-
// Relative path to your components folder
89-
$relativePath = '_components';
90-
9178
Configuration::make($environment)
92-
->setTemplatesPath($relativePath)
93-
->setTemplatesExtension('twig')
94-
->useCustomTags()
79+
->setTemplatesPath('/relative/directory/to/components')
9580
->setup();
9681
}
9782
}
9883
```
9984

100-
### OctoberCMS / WinterCMS
85+
### Configuration for October CMS / Winter CMS
10186

102-
In OctoberCMS / WinterCMS you need to hook into ```cms.page.beforedisplay``` event inside your plugin's boot method in order to access twig instance.
103-
Then you can use your plugin hint path to choose a views subfolder as component's folder.
87+
In October CMS and Winter CMS you need to hook into `cms.page.beforedisplay` event inside your plugin's boot method in order to access Twig instance.
88+
Then you can use your plugin hint path to choose a `views` subfolder as components' folder.
10489

105-
## Usage
106-
es.
107-
```
90+
```text
10891
plugins
10992
|__namespace
11093
|__pluginname
11194
|__views
11295
|__components
113-
|__button.htm
96+
|__button.twig
11497
```
11598

11699
```php
117-
public function boot(): void
118-
{
119-
Event::Listen('cms.page.beforeDisplay', function ($controller, $url, $page) {
120-
$twig = $controller->getTwig();
121-
122-
Configuration::make($twig)
123-
->setTemplatesPath('namespace.pluginname::components', hint: true)
124-
->useCustomTags()
125-
->useGlobalContext() // use this to keep twig context from cms
126-
->setup();
127-
});
128-
}
129-
```
130-
131-
then in your htm files
132-
133-
```
134-
<x-button>...</x-button>
100+
public function boot(): void
101+
{
102+
Event::Listen('cms.page.beforeDisplay', function ($controller, $url, $page) {
103+
$twig = $controller->getTwig();
104+
Configuration::make($twig)
105+
->setTemplatesPath('namespace.pluginname::components', hint: true)
106+
->useGlobalContext() // use this to keep Twig context from CMS
107+
->setup();
108+
});
109+
}
135110
```
136111

137-
Alle features like subfolders are supported, like ```<x-forms.input></x-forms.input>``` will refer to ```plugins/namespace/pluginname/views/forms/input.htm```
112+
> All features, like subfolders are supported. For example `<x-forms.input></x-forms.input>` will refer to `plugins/namespace/pluginname/views/forms/input.twig`.
138113
114+
## Usage
139115

140-
The components are just Twig templates in a folder of your choice (e.g. `components`) and can be used anywhere in your Twig templates. The slot variable is any content you will add between the opening and the close tag.
116+
Components are just Twig templates in a folder of your choice (e.g. `/components`) and can be used anywhere in your Twig templates:
141117

142118
```twig
143119
{# /components/button.twig #}
@@ -146,9 +122,9 @@ The components are just Twig templates in a folder of your choice (e.g. `compone
146122
</button>
147123
```
148124

149-
### Custom syntax
125+
> The slot variable is any content you will add between the opening and the close tag.
150126
151-
To reach a component you need to use custom tag `x` followed by a `:` and the filename of your component.
127+
To reach a component you need to use the dedicated tag `x` followed by `:` and the filename of your component without extension:
152128

153129
```twig
154130
{# /index.twig #}
@@ -157,26 +133,69 @@ To reach a component you need to use custom tag `x` followed by a `:` and the fi
157133
{% endx %}
158134
```
159135

136+
It will render:
137+
138+
```twig
139+
<button>
140+
<strong>Click me</strong>
141+
</button>
142+
```
143+
144+
### Custom tags
145+
146+
The same behaviour can be obtained with a **special HTML syntax**.
147+
148+
```php
149+
Configuration::make($twig)
150+
->setTemplatesPath('/relative/directory/to/components')
151+
->useCustomTags()
152+
->setup();
153+
```
154+
155+
```twig
156+
{# /index.twig #}
157+
<x-button class="text-white">
158+
<strong>Click me</strong>
159+
</x-button>
160+
```
161+
162+
### Attributes
163+
160164
You can also pass any params like you would using an `include`. The benefit is that you will have the powerful `attributes` variable to merge attributes or to change your component behaviour.
161165

162166
```twig
163167
{# /components/button.twig #}
164-
<button {{ attributes.merge({ class: 'rounded px-4' }) }}>
168+
<button {{ attributes.merge({class: 'rounded px-4'}) }}>
165169
{{ slot }}
166170
</button>
167171
168172
{# /index.twig #}
169-
{% x:button with {'class': 'text-white'} %}
173+
{% x:button with {class: 'text-white'} %}
170174
<strong>Click me</strong>
171175
{% endx %}
172176
173177
{# Rendered #}
174-
<button class="text-white rounded-md px-4 py-2">
178+
<button class="text-white rounded px-4">
175179
<strong>Click me</strong>
176180
</button>
177181
```
178182

179-
To reach components that are in **sub-folders** you can use _dot-notation_ syntax.
183+
With **custom tags** you can pass any attribute to the component in different ways. To interprate the content as Twig you need to prepend the attribute name with a `:`, but it works also in other ways.
184+
185+
```twig
186+
<x-button
187+
:any="'evaluate' ~ 'twig'"
188+
other="{{'this' ~ 'works' ~ 'too'}}"
189+
another="or this"
190+
this="{{'this' ~ 'does'}}{{'not work'}}"
191+
>
192+
Submit
193+
</x-button>
194+
```
195+
196+
### Subfolders
197+
198+
To reach components in subfolders you can use _dot-notation_ syntax.
180199

181200
```twig
182201
{# /components/button/primary.twig #}
@@ -190,19 +209,10 @@ To reach components that are in **sub-folders** you can use _dot-notation_ synta
190209
{% endx %}
191210
```
192211

193-
### HTML syntax
194-
195-
The same behaviour can be obtained with a special HTML syntax. The previus component example can alse be used in this way.
196-
197-
```twig
198-
{# /index.twig #}
199-
<x-button class='bg-blue-600'>
200-
<span class="text-lg">Click here!</span>
201-
</x-button>
202-
```
203-
204212
### Named slots
205213

214+
In case of use of multiple slots you can name them.
215+
206216
```twig
207217
{# /components/card.twig #}
208218
<div {{ attributes.class('bg-white shadow p-3 rounded') }}>
@@ -213,72 +223,62 @@ The same behaviour can be obtained with a special HTML syntax. The previus compo
213223
{{ body }}
214224
</div>
215225
</div>
216-
217-
{# /index.twig #}
218-
<x-card>
219-
<x-slot name="title" class="text-2xl">title</x-slot>
220-
<x-slot name="body">Body text</x-slot>
221-
</x-card>
222226
```
223227

224-
Also with the standard syntax.
228+
Use with standard syntax:
225229

226230
```twig
227231
{# /index.twig #}
228232
{% x:card %}
229-
{% slot:title with {class: "text-2xl"} %}
230-
Title
231-
{% endslot %}
232-
{% slot:body %}
233-
Title
234-
{% endslot %}
233+
{% slot:title with {class: 'text-2xl'} %}Title{% endslot %}
234+
{% slot:body %}Body{% endslot %}
235235
{% endx %}
236236
```
237237

238-
### Attributes
239-
240-
You can pass any attribute to the component in different ways. To interprate the content as Twig you need to prepend the attribute name with a `:` but it works also in other ways.
238+
Use with custom tags syntax:
241239

242240
```twig
243-
<x-button
244-
:any="'evaluate' ~ 'twig'"
245-
other="{{'this' ~ 'works' ~ 'too'}}"
246-
another="or this"
247-
this="{{'this' ~ 'does'}}{{ 'not work' }}"
248-
>
249-
Submit
250-
</x-button>
241+
{# /index.twig #}
242+
<x-card>
243+
<x-slot name="title" class="text-2xl">Title</x-slot>
244+
<x-slot name="body">Body</x-slot>
245+
</x-card>
251246
```
252247

253-
### Twig Namespaces
248+
### Twig namespaces
254249

255-
In addition to the specified directory, you can also reference components from a Twig namespace by prepending the namespace and a `:` to the component name. With a namespace defined like so:
250+
In addition to the specified directory, you can also reference components from a Twig namespace by prepending the component name with `<namespace>:`.
256251

257252
```php
258-
// register namespace with twig template loader
253+
// register namespace with Twig templates loader
259254
$loader->addPath(__DIR__ . '/some/other/dir', 'ns');
260255
```
261256

262-
Components can be included with the following:
257+
Use with standard syntax:
263258

264259
```twig
265-
{% x:ns:button with {class:'bg-blue-600'} %}
266-
<span class="text-lg">Click here!</span>
260+
{% x:ns:button with {class: 'bg-blue-600'} %}
261+
<strong>Click me</strong>
267262
{% endx %}
263+
```
268264

269-
{# or #}
265+
Use with custom tags syntax:
270266

271-
<x-ns:button class='bg-blue-600'>
272-
<span class="text-lg">Click here!</span>
267+
```twig
268+
<x-ns:button class="bg-blue-600">
269+
<strong>Click me</strong>
273270
</x-ns:button>
274271
```
275272

276-
### Dynamic Components
277-
Sometimes you may need to render a component but not know which component should be rendered until runtime. In this situation, you may use the built-in dynamic-component component to render the component based on a runtime value or variable:
273+
### Dynamic components
274+
275+
Sometimes you may need to render a component but not know which component should be rendered until runtime.
276+
In this situation, you may use the built-in `dynamic-component` to render the component based on a runtime value or variable:
277+
278278
```twig
279279
{% set componentName = 'button' %}
280-
<x-dynamic-component :component="componentName" class='bg-blue-600'>
281-
<span class="text-lg">Click here!</span>
280+
<x-dynamic-component :component="componentName" class="bg-blue-600">
281+
<strong>Click me</strong>
282282
</x-dynamic-component>
283283
```
284284

0 commit comments

Comments
 (0)