wp-strap/view

PSR模板加载器,用于WordPress插件。

0.1.1 2023-07-07 09:21 UTC

This package is auto-updated.

Last update: 2024-09-08 07:51:00 UTC


README

将依赖项安装到您的项目中。

composer require wp-strap/view

这公开了一些负责在插件中加载模板文件的类(但也可用于主题)。

这些类遵循PSR实践,使用接口,因此可以通过依赖注入和IoC容器进行OOP集成。它还提供了一个门面类,允许您使用静态方法而不是在您喜欢的地方调用方法。

使用门面的示例

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'
])

使用League Container的示例

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"文件夹定位模板文件(例如:your-plugin-folder/views)。

$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', 
]);

域文件夹

如果您有如下的域文件夹结构

my-custom-plugin/
├── src/                  
│   ├── Blocks/
│   │    └── Static/     
│   │         ├── css/  
│   │         ├── js/  
│   │         ├── views/  
│   │         │    ├── example-block.php  
│   │         │    └── another-example-block.php
│   │         └── images/  
│   ├── Admin/             
│   │    ├── Admin.php
│   │    └── Static/
│   │         ├── css/  
│   │         ├── js/  
│   │         ├── views/  
│   │         │    ├── admin-page.php  
│   │         │    └── another-admin-page.php
│   │         └── images/  
│   ├── Main/        
│   │    ├── Main.php     
│   │    └── Static/
│   │         ├── css/  
│   │         ├── js/  
│   │         └── images/  

您可以使用render()方法的第一个参数来指向域文件夹

$views->register([
    'dir' => plugin_dir_path(__FILE__), 
    'path' => 'src'
 ]);
 
echo $views->render('Blocks', 'my-example-block')->args([
    'my-argument' => 'my-value'
]);

如果您为"Static"使用其他名称,您可以使用"entry"设置将其更改为其他名称

$views->register([
    'dir' => plugin_dir_path(__FILE__), 
    'path' => 'src',
    'entry' => 'templates'
 ]);