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: docs/1-essentials/02-views.md
+78-1Lines changed: 78 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -622,6 +622,83 @@ Tempest views are always compiled to plain PHP code before being rendered. Durin
622
622
623
623
During deployments, that cache must be cleared in order to not serve outdated views to users. You may do that by running `tempest view:clear` on every deploy.
624
624
625
+
## Tempest View as a standalone engine
626
+
627
+
Tempest View is also designed to be used as a standalone engine in whatever PHP project you want. Start by requiring `tempest/view`:
628
+
629
+
```sh
630
+
composer require tempest/view
631
+
```
632
+
633
+
As a bare minimum setup, you can create an instance of the renderer by calling `TempestViewRenderer::make()`:
If, however, you want view component support, you will need to provide a `ViewConfig` object as well:
645
+
646
+
```php
647
+
use Tempest\View\Renderers\TempestViewRenderer;
648
+
use Tempest\View\ViewConfig;
649
+
650
+
$config = new ViewConfig()->addViewComponents(
651
+
__DIR__ . '/components/x-base.view.php',
652
+
__DIR__ . '/components/x-footer.view.php',
653
+
__DIR__ . '/components/x-header.view.php',
654
+
);
655
+
656
+
$renderer = TempestViewRenderer::make($config);
657
+
```
658
+
659
+
If you want to rely on Tempest's discovery to find view components, you can boot a minimal version of Tempest, and resolve the view renderer from the container:
You can choose whichever way you prefer. Chances are that, if you use the minimal setup without booting Tempest, you'll want to add a custom view component loader. That's up to you to implement then.
673
+
674
+
### A note on caching
675
+
676
+
When you're using the minimal setup, view caching can be enabled by passing in a `$viewCache` paremeter into `TempestViewRenderer::make()`:
677
+
678
+
```php
679
+
use Tempest\View\Renderers\TempestViewRenderer;
680
+
use Tempest\View\ViewCache;
681
+
682
+
$renderer = TempestViewRenderer::make(
683
+
cache: ViewCache::enabled(),
684
+
);
685
+
```
686
+
687
+
It's recommended to turn view caching on in production environments. To clear the view cache, you can call the `clear()` method on the `ViewCache` object:
688
+
689
+
```php
690
+
use Tempest\View\Renderers\TempestViewRenderer;
691
+
use Tempest\View\ViewCache;
692
+
693
+
$viewCache = ViewCache::enabled();
694
+
695
+
$viewCache->clear();
696
+
697
+
$renderer = TempestViewRenderer::make(
698
+
cache: $viewCache,
699
+
);
700
+
```
701
+
625
702
## Separate view directories
626
703
627
704
View files can live in any directory that is discoverable by Tempest. That means: a directory with a PSR-4 namespace associated with it. If you want your view files to live outside of `src` or `app`, you can add a namespace for it in composer.json:
@@ -637,7 +714,7 @@ View files can live in any directory that is discoverable by Tempest. That means
637
714
638
715
Don't forget to run `composer up` after making changes to your composer.json file.
639
716
640
-
Note that view files themselves don't need a namespace; this namespace is only here to tell Tempest that `views/` is a directory it should scan. If you want to add a class in the `Views` namespace (like, for example, a [custom view object](/2.x/essentials/views#using-dedicated-view-objects)), then that is possible as well.
717
+
Note that view files themselves don't need a namespace; this namespace is only here to tell Tempest that `views/` is a directory it should scan. If you want to add a class in the `Views` namespace (like, for example, a [custom view object](/2.x/essentials/views#using-dedicated-view-objects)), then that is possible as well.
There are a couple of notes to make when running Tempest View as a standalone component:
115
-
116
-
- Any view files and components will be discovered and must be in a directory with a valid PSR-4 namespace. View files themselves don't need to have a namespace, though.
117
-
- View files are compiled and cached. You can manually enable or disable this cache by setting the `{env}{:hl-keyword:VIEW_CACHE:}` environment variable to `true` or `false`. By default, the view cache is disabled.
118
-
- Optionally, you can require `tempest/console`, which will provide you with the `vendor/bin/tempest view:clear` command to clear view caches. If you don't install `tempest/console`, you'll have to manually clear view caches on deployment by removing the `.tempest/cache/views` directory.
100
+
Tempest View can be used as a standalone package. You can read about how to use it [here](/2.x/essentials/views#tempest-view-as-a-standalone-engine).
0 commit comments