You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Web/Documentation/content/main/1-framework/04-views.md
+65Lines changed: 65 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -377,6 +377,46 @@ A `$slot` variable is an instance of `\Tempest\View\Slot`, and has a handful of
377
377
-`$slot->attributes`: all the attributes defined on the slot, they can also be accessed directly via `$slot->attributeName`
378
378
-`$slot->content`: the compiled content of the slot
379
379
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`.
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`.
<!-- <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:
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
541
581
<div>Post C</div>
542
582
```
543
583
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
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