Skip to content

Commit 8a3c63b

Browse files
committed
wip
1 parent 97cdc6c commit 8a3c63b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

docs/1-essentials/02-views.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,67 @@ Don't forget to run `composer up` after making changes to your composer.json fil
639639

640640
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.
641641

642+
## Tempest View as a standalone engine
643+
644+
Tempest View is also designed to be used as a standalone engine in whatever PHP project you want. Start by requiring `tempest/view`:
645+
646+
```sh
647+
composer require tempest/view
648+
```
649+
650+
As a bare minimum setup, you can create an instance of the renderer by calling `TempestViewRenderer::make()`:
651+
652+
```php
653+
use Tempest\View\Renderers\TempestViewRenderer;
654+
use function Tempest\view;
655+
656+
$renderer = TempestViewRenderer::make();
657+
658+
$html = $renderer->render(view('home.view.php', name: 'Brent'));
659+
```
660+
661+
If, however, you want view component support, you will need to provide a `ViewConfig` object as well:
662+
663+
```php
664+
use Tempest\View\Renderers\TempestViewRenderer;
665+
use Tempest\View\ViewConfig;
666+
667+
$config = new ViewConfig()->addViewComponents(
668+
__DIR__ . '/components/x-base.view.php',
669+
__DIR__ . '/components/x-footer.view.php',
670+
__DIR__ . '/components/x-header.view.php',
671+
);
672+
673+
$renderer = TempestViewRenderer::make($config);
674+
```
675+
676+
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:
677+
678+
```php
679+
use Tempest\Core\Tempest;
680+
use Tempest\View\ViewRenderer;
681+
682+
$container = Tempest::boot(__DIR__);
683+
684+
$html = $container->get(ViewRenderer::class)->render(
685+
view('home.view.php', name: 'Brent')
686+
);
687+
```
688+
689+
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.
690+
691+
### A note on caching
692+
693+
When you're using the minimal setup, view caching can be enabled by passing in a `$cache` paremeter into `TempestViewRenderer::make()`:
694+
695+
```php
696+
$renderer = TempestViewRenderer::make(
697+
cache: true,
698+
);
699+
```
700+
701+
It's recommended to turn view caching on in production environments. To clear the view cache, you'll have to manually delete the cache directory, which is located at `./vendor/tempest/view/.tempest/cache`.
702+
642703
## Using other engines
643704

644705
While Tempest View is simple to use, it currently lacks tooling support from editors and IDEs. You may also simply prefer other templating engines. For these reasons, you may use any other engine of your choice.

0 commit comments

Comments
 (0)