TypeRocket v1 Pro users can now upgrade to TypeRocket v5 at their convenience.
Views
( v1 )
- # Making a View
- # Admin Views
- # Front-end Views
- # View Page Title
- # SEO Meta
- # Templating
- # Twig Engine
- # Clear Twig Cache
- # Customizing Twig Env
- # Multiple Templating Engines
- # Views In Templates
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. -
$extension
- The default is.php
.
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');
SEO Meta
If you are using views within a post type's template file, you do not need to set SEO meta of the view. However, if you view is for anything that is not a post type you will need to set the SEO meta using the setSeoMeta()
view method.
If the view has access to the TypeRocket SEO meta from the \TypeRocket\Extensions\SEO
extension, you can set the SEO for the page.
tr_view('books.show')->setSeoMeta($meta, $url);
You can add the SEO fields to a custom backend using the tr_seo_meta_fields()
function. The function requires a Form
instance.
tr_seo_meta_fields($form);
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 has 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 Engine
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.
|
*/
'templates' => [
'views' => '\TypeRocket\Template\TwigTemplateEngine',
'pages' => '\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' => immutable('WP_DEBUG', true),
'cache' => tr_config('paths.cache') . '/twig',
]
];
Multiple Templating Engines
If you want to use an engine that is not the default, you can use the setEngine()
method.
$engine = '\TypeRocket\Template\TwigTemplateEngine';
tr_view('books.show')->setEngine($engine);
Views In Templates
You can also use views within your standard WordPress templates. To use views for your WordPress templates use the \TypeRocket\Http\Template::respond()
static method.
<?php
/**
* Example WordPress Template MVC
*
* my-theme/index.php
*
* @var WP_Post[] $posts
*/
\TypeRocket\Http\Template::respond(function() use ($posts) {
$title = 'Pro';
return tr_view('master', compact('posts', 'title'));
});
When using the \TypeRocket\Http\Template::respond()
static method, you are able to exit the global scope and avoid conflicting with global variables. To access any global variables, you must pass them using the use
statement.
Found a typo? Something is wrong in this documentation? Fork and edit it!