New! TypeRocket v5 is now available. See docs.
Access all of Typerocket. Get Pro.
Views
( v4 )
- # Making a View
- # Admin Views
- # Front-end Views
- # View Page Title
- # Templating
- # Twig
- # Clear Twig Cache
- # Customizing Twig Env
Making a View
To create a View
use the tr_view()
helper function. This function takes two arguments:
-
$view
- The view to be returned. This uses dot notation. -
$data
- An array of values to be extracted in the view.
tr_view('books.index', ['data' => 'my data']);
Because a view uses dot notation it can return different templates based on context. These contexts are: "Admin" and "Front-end".
Admin Views
Admin views are located under resources/pages
by default. When a controller returns a view in the admin context it will return the view from the specified folder.
Take this example view, in the admin context it will return the resources/pages/books/index.php
template file.
tr_view('books.index', ['data' => 'my data']);
Front-end Views
Front-end views are located under resources/views
by default. When a controller returns a view in the front-end context it will return the view from the specified folder.
Take this example view, in the front-end context it will return the resources/views/books/index.php
template file.
tr_view('books.index', ['data' => 'my data']);
View Page Title
To set a views page <head>
<title>
tag use the setTitle()
method.
tr_view('books.add')->setTitle('Add Book');
Templating
When a view is loaded like resources/views/books/index.php
and resources/pages/books/index.php
the passes array will be unpacked using the php extract()
function.
So, a view with ['data' => 'my data']
passed to it have a $data
variable with the string 'my data'
in its template file.
// index.php
echo '<p>' . $data . '</p>';
Will output,
<p>my data</p>
Twig
If you want to use Twig for your views require Twig as apart of your project using composer
.
composer require "twig/twig:^2.0"
Next, update your template_engine
settings in your config/app.php
file and switch your engine class to \TypeRocket\Template\TwigTemplateEngine::class
.
/*
|--------------------------------------------------------------------------
| Template Engine
|--------------------------------------------------------------------------
|
| The template engine used to build views for the front-end and admin.
|
*/
'template_engine' => [
'front' => '\TypeRocket\Template\TwigTemplateEngine',
'admin' => '\TypeRocket\Template\TwigTemplateEngine',
],
Instead of naming your template files in the .php
extension, use the .twig
extension.
Clear Twig Cache
To clear the twig cache using galaxy
.
galaxy cache:clear twig
Note: If WP_DEBUG
is enabled caching will be disabled but the cache still needs to be cleared.
Customizing Twig Env
To customize the Twig config add a new config file named config/twig.php
with the following code:
return [
'env' => [
'debug' => WP_DEBUG,
'cache' => \TypeRocket\Core\Config::locate('paths.cache') . '/twig',
]
];
Found a typo? Something is wrong in this documentation? Fork and edit it!