1. Go to this page and download the library: Download wp-strap/view library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wp-strap / view example snippets
use WPStrap\View\View;
// Resolves instance and registers project configurations
View::register([
'dir' => plugin_dir_path(__FILE__), // or get_stylesheet_directory() for themes
]);
echo View::render('my-component')->args([
'my-argument' => 'my-value'
])
use WPStrap\View\View;
use WPStrap\View\ViewService;
// Instantiates the Asset service and registers project configurations
$views = new ViewService();
$views->register([
'dir' => plugin_dir_path(__FILE__), // or get_stylesheet_directory() for themes
]);
// Renders template with arguments
echo $views->render('my-component')->args([
'my-argument' => 'my-value'
])
// You can also use the facade based on this instance.
View::setFacade($views);
View::render('my-second-component');
use WPStrap\View\ViewInterface;
use WPStrap\View\ViewService;
function views(): ViewsInterface {
static $views;
if(!isset($views)) {
$views = (new ViewService())->register([
'dir' => plugin_dir_path(__FILE__),
]);
}
return $views;
}
echo views()->render('my-component')->args([
'my-argument' => 'my-value'
])
use League\Container\Container;
use WPStrap\View\View;
use WPStrap\View\ViewInterface;
use WPStrap\View\ViewService;
$container = new Container();
$container->add(ViewsInterface::class)->setConcrete(ViewService::class)->addMethodCall('register', [
'dir' => plugin_dir_path(__FILE__),
]);
$views = $container->get(ViewsInterface::class);
echo $views->render('my-component')->args([
'my-argument' => 'my-value'
])
// You can also set a PSR container as facade accessor
View::setFacadeAccessor($container);
View::render('my-second-component');
$views->register([
// Determines the root dir of your project
'dir' => plugin_dir_path(__FILE__),
// Will change templates path to "your-plugin-folder/path-to-views/views"
'path' => 'path-to-views',
// Changes "your-plugin-folder/views" to "your-plugin-folder/templates"
'folder' => 'templates',
// Will override templates from theme/child-themes if templates exist in the
// "clients-theme/my-plugin-name" directory, this is turned off by default
// to remove the performance load since it won't be needed for most plugins
'locate' => 'my-plugin-name',
// By default it uses the plugin folder name as hook prefix for filters inside the
// classes (eg: a filter for "my-plugin-folder" becomes "my_plugin_folder_view_args")
// With this setting you can change the prefix
'hook' => 'my_plugin_hook',
]);