Access all of Typerocket. Get Pro.
Views
( v5 )
- Making a View
- Admin Views & Front-end Views
- View Page Title
- SEO Meta
- Templating
- Twig Engine
- Clear Twig Cache
- Customizing Twig Env
- Tachyon Template Engine
- 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 & Front-end Views
Views are located under resources/views
by default. When a controller returns a view it will return the view from the specified folder.
Take this example view, 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
Pro Only: This is a Pro only extension feature.
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 \TypeRocketPro\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
Pro Only: This is a Pro only extension feature.
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' => '\TypeRocketPro\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' => typerocket_env('WP_DEBUG', true),
'cache' => tr_config('paths.cache') . '/twig',
]
];
Tachyon Template Engine
Pro Only: This is a Pro only extension feature.
TypeRocket Pro also has a template engine called Tachyon. This template engine is a blazing fast and pure PHP templating engine equipped with layouts and more. You can read more about Tachyon in the theme templating docs.
Multiple Templating Engines
If you want to use an engine that is not the default, you can use the setEngine()
method.
$engine = '\TypeRocketPro\Template\TwigTemplateEngine';
tr_view('books.show')->setEngine($engine);
Views In Templates
Pro Only: This is a Pro only extension feature.
You can also use views within your standard WordPress templates. To use views for your WordPress templates use the tr_template_router
function.
<?php
/**
* Example WordPress Template MVC
*
* my-theme/index.php
*
* @var WP_Post[] $posts
*/
tr_template_router(function() use ($posts) {
$title = 'Pro';
return tr_view('master', compact('posts', 'title'));
});
When using the tr_template_router
function, 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!