pinkcrab/bladeone-engine

用于PinkCrab插件框架V2+的PinkCrab可渲染接口的实现。


README

BladeOne_Engine

为PinkCrab可渲染接口提供BladeOne引擎。

Latest Stable Version Total Downloads License PHP Version Require GitHub contributors GitHub issues

WP5.9 [PHP7.4-8.1] Tests WP6.0 [PHP7.4-8.1] Tests WP6.1 [PHP7.4-8.2] Tests WP6.2 [PHP7.4-8.2] Tests

codecov Scrutinizer Code Quality Maintainability

支持并测试了PinkCrab Perique框架版本2.0.*

为什么?

BladeOne对可渲染接口的实现,允许在PinkCrab框架中使用Blade。

设置

$ composer require pinkcrab/bladeone-engine

开箱即用,您只需在启动Perique时包含BladeOne模块,您就可以完全访问BladeOne引擎。

// Bootstrap for Perique follows as normal.. 
$app = ( new App_Factory('path/to/project/root') )
   ->default_config()
   ->module(BladeOne::class)
   // Rest of setup
   ->boot();

默认情况下,以下内容被假定

  • path/to/project/root/views作为视图路径
  • path/wp-content/uploads/blade-cache作为缓存路径
  • MODE_AUTO

用法

就像在Perique中找到的本地PHP_Engine一样,您可以将视图服务注入任何类,并使用它来渲染视图。

class Some_Class {
   
      public function __construct( View $view ) {
         $this->view = $view;
      }
   
      public function render_view() {
         return $this->view->render('some.path.view_name', ['data' => 'to pass']);
      }
}

上述代码将渲染模板path/to/project/root/views/some/path/view_name.blade.php,并访问视图中传递的$data,该值为to pass

<p>{{ $data }}</p>

将渲染为

<p>to pass</p>

完全可以利用模板继承和其他Blade功能。

<div class="wrap">
   @include('partials.header')
   @yield('content')
   @include('partials.footer')
</div>
@extends('layouts.default')
@section('content')
   Some content
@stop

配置BladeOne

与其他所有模块一样,BladeOne可以通过将\Closure作为module()方法的第二个参数传递来进行配置。

// Bootstrap for Perique follows as normal..
$app = ( new App_Factory('path/to/project/root') )
   ->default_config()
   ->module(BladeOne::class, function( BladeOne $blade ) {
      // Module config.
      $blade
         ->template_path('path/to/custom/views')
         ->compiled_path('path/to/custom/cache'); // Fluent API for chaining.
      
      $blade->mode( BladeOne::MODE_DEBUG );

      // BladeOne_Engine config.
      $blade->config( function( BladeOne_Engine $engine  {
         // See all methods below.
         $engine
            ->set_compiled_extension('.php')
            ->directive('test', fn($e) =>'test'); // Fluent API for chaining.
         
         $engine->allow_pipe( false ); 
      });

      // Ensure you return the instance.
      return $blade;
   })
   // Rest of setup
   ->boot();
紧凑BladeOne配置

可以使用流畅API和PHP箭头函数以更简洁的方式执行模块配置

$app = ( new App_Factory('path/to/project/root') )
   ->default_config()
   ->module(BladeOne::class, fn( BladeOne $blade ) => $blade
      ->template_path('path/to/custom/views')
      ->compiled_path('path/to/custom/cache')
      ->mode( BladeOne::MODE_DEBUG )
      ->config( fn( BladeOne_Engine $engine ) => $engine
         ->set_compiled_extension('.php')
         ->directive('test', fn($e) =>'test')
         ->allow_pipe( false )
      )
   )
->boot();

您还可以将其配置保存在自己的类中并使用该类。

/** Some Class */
class BladeOneConfig {
   public function __invoke( BladeOne $blade ): BladeOne {
      return $blade
         // The setup.
   }
}

$app = ( new App_Factory('path/to/project/root') )
   ->default_config()
   ->module(BladeOne::class, new BladeOneConfig() )
   ->boot();

BladeOne_Module配置

您可以通过以下方法调用BladeOne模块来配置BladeOne模块。

BladeOne_Engine配置

您可以通过以下方法调用BladeOne_Engine来配置BladeOne_Engine。

Blade模板

大多数Blade功能都存在,要查看完整文档,请访问EFTEC/BladeOne wiki

包含的组件

PinkCrab_BladeOne开箱即用附带BladeOneHTML特例,提供所有HTML组件的访问权限。

魔术调用方法

BladeOne类具有大量静态和常规方法,这些方法都可以从BladeOne_Engine访问。它们可以按如下方式调用。

// None static
$this->view->engine()->some_method($data);

// As static 
BladeOne_Engine::some_method($data);

它们也可以在模板中调用。

{$this->some_method($data)}

// Or
{BladeOne_Engine::some_method($data)}

有关方法列表的完整列表,请访问https://github.com/EFTEC/BladeOne/wiki/Methods-of-the-class

静态访问

// Using the App's View method to access none static methods on the fly.
App::view()->engine()->some_method($data);

在视图中调用 engine() 将返回所使用的底层渲染引擎,在本例中为 PinkCrab_BladeOne

当然,您可以使用 $provider->share('view_helper', [App::view(), 'engine']) 将引擎本身设置为全局变量。然后您可以在视图中使用 {$view_helper->some_method($data)}

扩展

如果想要通过添加自定义指令或向 BladeOne_Engine 类添加额外的方法来添加额外的功能,可以使用其他插件扩展 BladeOne。您可以通过使用 PinkCrab_BladeOne::SETUP_CONFIG 动作并添加任何额外的配置,如指令来实现。

add_action( PinkCrab_BladeOne::SETUP_CONFIG, function( PinkCrab_BladeOne $engine ) {
    $engine->directive( 'my_directive', function( $expression ) {
        return "<?php echo 'Hello World'; ?>";
    } );
} );

依赖关系

要求

许可

MIT 许可证

https://open-source.org.cn/licenses/mit-license.html

之前 Perique 支持

  • 对于 Perique V2 之前所有版本的兼容性支持,请使用 BladeOne_Provider

变更日志

  • 1.0.0 - 从 BladeOne_Provider 的 Perique V2 预分支迁移过来。
    • 新功能
    • 认证和权限现在基于当前用户连接。
    • Perique V2 模块结构。
    • 支持 WP Nonce。