Skip to content

Commit 21b8a8a

Browse files
committed
update
1 parent b56ea2f commit 21b8a8a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

app/Front/Docs/Content/framework/04-views.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ Note that `{html}class` and `{html}style` attributes will be merged together, wh
436436
<!-- <div class="bar foo" style="text-decoration: underline; background: red;" id="overwrite"></div> -->
437437
```
438438

439-
440439
## View component classes
441440

442441
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.
@@ -601,6 +600,31 @@ Will be compiled to
601600
<div>Post C</div>
602601
```
603602

603+
## View processors
604+
605+
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`:
606+
607+
```php
608+
use Tempest\View\View;
609+
use Tempest\View\ViewProcessor;
610+
611+
class StarCountViewProcessor implements ViewProcessor
612+
{
613+
public function __construct(
614+
private Github $github,
615+
) {}
616+
617+
public function process(View $view): View
618+
{
619+
if (! $view instanceof WithStarCount) {
620+
return $view;
621+
}
622+
623+
return $view->data(starCount: $this->github->getStarCount());
624+
}
625+
}
626+
```
627+
604628
## View caching
605629

606630
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)