wp-strap/view 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wp-strap/view

最新稳定版本:0.1.1

Composer 安装命令:

composer require wp-strap/view

包简介

PSR Template loader to use in WordPress plugins.

README 文档

README

Install dependency into your project.

composer require wp-strap/view

This exposes some classes which are responsible loading template files inside plugins (but can also be used for themes).

The classes follow PSR practices with interfaces, so it can be included trough OOP with dependency injection and IoC containers. It also provides a Facade class that allows you to use static methods instead to call the methods everywhere you like.

Example with using the facade:

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

Example with using the instance

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

Example with using instance as function

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

Example with using the 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');

Base settings

It locates the template files from the "views" folder by default (eg: 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', 
]);

Domain folder

If you have a domain folder structure like this

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/  

You can use the first param of the render() method to point to the domain folder

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

If you use another name for "Static" you can use the "entry" setting to change it to something else

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

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2023-07-07