Skip to content

Commit 56ee382

Browse files
committed
feat: add more view documentation
1 parent ac963a5 commit 56ee382

File tree

1 file changed

+65
-0
lines changed
  • src/Web/Documentation/content/main/1-framework

1 file changed

+65
-0
lines changed

src/Web/Documentation/content/main/1-framework/04-views.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,46 @@ A `$slot` variable is an instance of `\Tempest\View\Slot`, and has a handful of
377377
- `$slot->attributes`: all the attributes defined on the slot, they can also be accessed directly via `$slot->attributeName`
378378
- `$slot->content`: the compiled content of the slot
379379

380+
### Dynamic attributes
381+
382+
Besides `$slots`, there will also be a variable called `$attributes` in every view component, which allows you to dynamically access the attributes that were passed to a view component. Note that only data attributes will be accessible, expression attributes will not. Also, attribute names will always be written as `kebab-case`.
383+
384+
```html
385+
<x-component name="x-with-attributes">
386+
<div x-data="custom {{ $attributes['x-data'] }}"></div>
387+
</x-component>
388+
389+
<x-with-attributes x-data="too"></x-with-attributes>
390+
391+
<!-- <div x-data="custom too"></div> -->
392+
```
393+
394+
### Fallthrough attributes
395+
396+
There are a handful of special attributes that will always be merged within the view component's root element: `{html}class`, `{html}style`, and `{html}id`.
397+
398+
```html
399+
<x-component name="x-with-fallthrough-attributes">
400+
<div></div>
401+
</x-component>
402+
403+
<x-with-fallthrough-attributes class="foo" style="background: red;" id="bar"></x-with-fallthrough-attributes>
404+
405+
<!-- <div class="foo" style="background: red;" id="bar"></div> -->
406+
```
407+
408+
Note that `{html}class` and `{html}style` attributes will be merged together, while the `id` attribute will overwrite any original value defined in the view component:
409+
410+
```html
411+
<x-component name="x-with-fallthrough-attributes">
412+
<div class="bar" style="text-decoration: underline;" id="original"></div>
413+
</x-component>
414+
415+
<x-with-fallthrough-attributes class="foo" style="background: red;" id="overwrite"></x-with-fallthrough-attributes>
416+
417+
<!-- <div class="bar foo" style="text-decoration: underline; background: red;" id="overwrite"></div> -->
418+
```
419+
380420
## View component classes
381421

382422
View components can live solely within a `.view.php` file, in which case they are called **anonymous view components**. However, it's also possible to define a class to represent a view component. One of the main benefits of doing so, is that **view component classes** are resolved via the container, meaning they can request any dependency available within your project, and Tempest will autowire it for you. View component classes are also discovered automatically, and must implement the `ViewComponent` interface.
@@ -541,6 +581,31 @@ Will be compiled to
541581
<div>Post C</div>
542582
```
543583

584+
## View processors
585+
586+
View processors are classes that manipulate a view before it is rendered. They are automatically discovered and can be used to add data to multiple view files at once. Here's an already complexer example of a view processor that will add a star count from GitHub to every view that implements `WithStarCount`:
587+
588+
```php
589+
use Tempest\View\View;
590+
use Tempest\View\ViewProcessor;
591+
592+
final class StarCountViewProcessor implements ViewProcessor
593+
{
594+
public function __construct(
595+
private readonly Github $github,
596+
) {}
597+
598+
public function process(View $view): View
599+
{
600+
if (! $view instanceof WithStarCount) {
601+
return $view;
602+
}
603+
604+
return $view->data(starCount: $this->github->getStarCount());
605+
}
606+
}
607+
```
608+
544609
## View caching
545610

546611
Tempest views are compiled to plain PHP code before being rendered. In production, these compiled views should be cached. Enabling the view cache on production can be done by setting the `{txt}{:hl-property:CACHE:}` environment variable:

0 commit comments

Comments
 (0)