PHPackages                             pinkcrab/bladeone-provider - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. pinkcrab/bladeone-provider

ActiveLibrary

pinkcrab/bladeone-provider
==========================

An implementation of the PinkCrab Renderable Interface used in the PinkCrab Plugin Framework.

1.4.1(3y ago)0584[1 issues](https://github.com/Pink-Crab/Perique-BladeOne-Provider/issues)MITPHPPHP &gt;=7.2.0

Since Jan 20Pushed 3y agoCompare

[ Source](https://github.com/Pink-Crab/Perique-BladeOne-Provider)[ Packagist](https://packagist.org/packages/pinkcrab/bladeone-provider)[ Docs](https://pinkcrab.co.uk)[ RSS](/packages/pinkcrab-bladeone-provider/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (15)Versions (31)Used By (0)

BladeOne\_Provider
==================

[](#bladeone_provider)

A BladeOne Provider for the PinkCrab Renderable Interface.

[![Latest Stable Version](https://camo.githubusercontent.com/3d1f1cbca75fee5bfa282372f9503c023d59e08ab5994ac9a5f24cb2f396fc54/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f626c6164656f6e652d70726f76696465722f76)](https://packagist.org/packages/pinkcrab/bladeone-provider)[![Total Downloads](https://camo.githubusercontent.com/715d6a87ab7215f9f3f39b7cd600e7e60b03437d5ad5fab5720eb574e3bbaaf9/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f626c6164656f6e652d70726f76696465722f646f776e6c6f616473)](https://packagist.org/packages/pinkcrab/bladeone-provider)[![License](https://camo.githubusercontent.com/33e542e2a11187afb7a61ae5f93b29b4e09539e75695bde3e1932fec980c2abd/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f626c6164656f6e652d70726f76696465722f6c6963656e7365)](https://packagist.org/packages/pinkcrab/bladeone-provider)[![PHP Version Require](https://camo.githubusercontent.com/b1bde3a96478c9f8218dc1c5094dd549b82a04bdeaaa759c4aacb41563f92957/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f626c6164656f6e652d70726f76696465722f726571756972652f706870)](https://packagist.org/packages/pinkcrab/bladeone-provider)[![GitHub contributors](https://camo.githubusercontent.com/f2be3bff317b31f50be547c78e09f31e8e99a972ec7b1c9a808026701da82bc1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f506572697175652d426c6164654f6e652d50726f76696465723f6c6162656c3d436f6e7472696275746f7273)](https://camo.githubusercontent.com/f2be3bff317b31f50be547c78e09f31e8e99a972ec7b1c9a808026701da82bc1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f506572697175652d426c6164654f6e652d50726f76696465723f6c6162656c3d436f6e7472696275746f7273)[![GitHub issues](https://camo.githubusercontent.com/96e6f75eb7daf78f794081c36f788f183386b268e29c9314bb05da82b1dcdf04/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f506572697175652d426c6164654f6e652d50726f7669646572)](https://camo.githubusercontent.com/96e6f75eb7daf78f794081c36f788f183386b268e29c9314bb05da82b1dcdf04/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f506572697175652d426c6164654f6e652d50726f7669646572)

[![WP5.9 [PHP7.2-8.1] Tests](https://github.com/Pink-Crab/Perique-BladeOne-Provider/actions/workflows/WP_5_9.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-BladeOne-Provider/actions/workflows/WP_5_9.yaml)[![WP6.0 [PHP7.2-8.1] Tests](https://github.com/Pink-Crab/Perique-BladeOne-Provider/actions/workflows/WP_6_0.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-BladeOne-Provider/actions/workflows/WP_6_0.yaml)[![WP6.1 [PHP7.2-8.1] Tests](https://github.com/Pink-Crab/Perique-BladeOne-Provider/actions/workflows/WP_6_1.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-BladeOne-Provider/actions/workflows/WP_6_1.yaml)

[![codecov](https://camo.githubusercontent.com/1ae6181af0104a485e926f9b9bfa327733ae56a9dbf10fcdba80c4ca5d1cdc6c/68747470733a2f2f636f6465636f762e696f2f67682f50696e6b2d437261622f506572697175652d426c6164654f6e652d50726f76696465722f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d4637573453394f354952)](https://codecov.io/gh/Pink-Crab/Perique-BladeOne-Provider)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/98637fddf79944f19abbc09cfe3b5613fcb64c9911346603cc329fa7b31aad6f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f50696e6b2d437261622f506572697175652d426c6164654f6e652d50726f76696465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Pink-Crab/Perique-BladeOne-Provider/?branch=master)[![Maintainability](https://camo.githubusercontent.com/9e91a7f842a06f545aea4da0e188d8b4236ecfe75571aa52d4d1a9cbcedd9a86/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35313635393065373534386561646565616138612f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Pink-Crab/Perique-BladeOne-Provider/maintainability)

> Supports and tested with the PinkCrab Perique Framework versions 1.4.\*

Why?
----

[](#why)

The BladeOne implementation of the Renderable interface, allows the use of Blade within the PinkCrab Framework.

Setup
-----

[](#setup)

```
$ composer require pinkcrab/bladeone-provider
```

The simplest way to enable BladeOne is to use the `BladeOne_Bootstrap` helper class, this will configure BladeOne fully for use as the `Renderable` implementation. To use this just include the following before Perique is setup in your plugin.php file for plugins or functions.php for themes.

```
/**
 * Bootstrap Blade into Perique
 *
 * @param string|array                             $views_path   The Path or paths used for the template files.
 * @param string                                   $cache_path   The path where all compiled/cached template files.
 * @param int|null                                 $blade_mode   The mode to start blade one using ( )
 * @param class_string | PinkCrab_BladeOne::class  $blade_class  The implementation of BladeOne to use
 */
BladeOne_Bootstrap::use( $views_path, $cache_path, $blade_mode, $blade_class );

// Bootstrap for Perique follows as normal..
$app = ( new App_Factory() )->with_wp_dice( true )
	->.....
```

> **$views\_path** :: This can be a string or an array of strings. If an array is passed, the first path that exists will be used. If not passed, the path defined in Perique will be used.

> **$cache\_path** :: This should be a string path to the cache directory. If not passed, the path will be set as the `WP_CONTENT_DIR` . 'uploads/compiled/blade'

> **$blade\_mode** :: For more details on the options please [see official docs](https://github.com/EFTEC/BladeOne/blob/d3e1efa1c6f776aa87fe47164d77e7ea67fc196f/lib/BladeOne.php#L208)

> **$blade\_class** :: This should be a the class name or instance of a class that extends `PinkCrab_BladeOne::class` this allows for the creation of custom components and extending BladeOne in general. For more details please [see official docs](https://github.com/EFTEC/BladeOne/wiki/Extending-the-class) Passing nothing or an invalid type will just use the default PinkCrab\_BladeOne.

> If the cache directory doesn't exist, BladeOne will create it for you. It is however best to do this yourself to be sure of permissions etc.

Included Components
-------------------

[](#included-components)

Out of the box PinkCrab\_BladeOne comes with the BladeOneHTML trait added, giving access all HTML components. [BladeOneHTML Docs](https://github.com/EFTEC/BladeOneHtml)

Configuring BladeOne
--------------------

[](#configuring-bladeone)

At its core BladeOne is a single class representation of Blade and most of its core functionality. To make configuration possible when being injected from DI Container we have a custom class you can extend and add to the registration array like any other Hookable class.

```
class My_Blade_Config extends Abstract_BladeOne_Config {

	// Services can be injected using DI as normal (with Perique)
	protected $service;
	public function __construct( Mock_Service $service ) {
		$this->service = $service;
	}

	/**
	 * This is the only method that must be implemented
	 * @param BladeOne_Provider $provider The instance of BladeOne being used.
	 */
	public function config( BladeOne_Provider $provider ): void {
		// Use this method to configure Blade
		// Details of methods can be found below.
		$provider->set_compiled_extension( $this->service->get_cache_file_extension() );
		$provider->directive( 'test', [ $this->service, 'some_method' ] );
		$provider->allow_pipe( false ); // Pipe is enabled by default, unlike standard BladeOne
	}
}
```

> You can have as many of these config classes as you want, allowing you to break up any custom directives, globals values and aliases etc.

Public Methods
--------------

[](#public-methods)

The BladeOne\_Provider class has a number of methods which can be used to configure the underlying BladeOne implementation. This can be done using the `config()` method as part of the Config class above.

---

### **allow\_pipe**

[](#allow_pipe)

```
/**
 * Sets if piping is enabled in templates.
 *
 * @param bool $bool
 * @return self
 */
public function allow_pipe( bool $bool = true ): self{}
```

Calling this will allow you toggle piping `{{ $var | esc_html }}` on or off. By default this is enabled.
*Details*: [https://github.com/EFTEC/BladeOne/wiki/Template-Pipes-\\(Filter\\)](https://github.com/EFTEC/BladeOne/wiki/Template-Pipes-%5C(Filter%5C))

---

### **directive**

[](#directive)

```
/**
 * Register a handler for custom directives.
 *
 * @param string   $name
 * @param callable $handler
 * @return self
 */
public function directive( string $name, callable $handler ): self{}
```

Calling this will allow you to create custom directives

```
// Directive Example
$provider->directive('datetime', function ($expression) {
	// Return a valid PHP expression in php tags
    return "";
});
```

```

@datetime($now)

01/24/2021 14:34
```

```
// You will need to pass $now to your view
$class->render('path.to.view', ['now' => new DateTime()]);
```

*Details*:

> Don't forget our config class is loaded via the DI Container, so you can encapsulate your Directive callbacks into a class, with dependencies injected using the DI Container. (See above example)

---

### **directive\_rt**

[](#directive_rt)

```
/**
 * Register a handler for custom directives at runtime only.
 *
 * @param string   $name
 * @param callable $handler
 * @return self
 */
public function directive_rt( string $name, callable $handler ): self{}
```

Calling this will allow you to create custom directives

```
// Directive at Run Time Example
$provider->directive_rt('datetime', function ($expression) {
	// Just print/echo the value.
	return "echo $expression->format('m/d/Y H:i');";
});
```

```

@datetime($now)

01/24/2021 14:34
```

```
// You will need to pass $now to your view
$class->render('path.to.view', ['now' => new DateTime()]);
```

---

### **add\_include**

[](#add_include)

```
/**
 * Define a template alias
 *
 * @param string      $view  example "folder.template"
 * @param string|null $alias example "mynewop". If null then it uses the name of the template.
 * @return self
 */
public function add_include( $view, $alias = null ): self{}
```

This will allow you to set alias for your templates, this is ideal for global variables (share()).

```
// Directive at Run Time Example
$provider->add_include('some.long.path.no.one.wants.to.type', 'longpath');

// This can then be used when rendering.
$class->render('longpath', ['data' => $data]);
```

---

### **add\_alias\_classes**

[](#add_alias_classes)

```
/**
 * Define a class with a namespace
 *
 * @param string $alias_name
 * @param string $class_with_namespace
 * @return self
 */
public function add_alias_classes( $alias_name, $class_with_namespace ): self{}
```

This allows for the creation of simpler and short class names for use in templates.

```
$provider->add_alias_classes('MyClass', 'Namespace\\For\\Class');
```

```

{{MyClass::some_method()}}
{!! MyClass::some_method() !!}
@MyClass::some_method()
```

---

### **share**

[](#share)

```
/**
 * Adds a global variable. If $var_name is an array then it merges all the values.
 * Example:
 *
 * $this->share('variable',10.5);
 * $this->share('variable2','hello');
 * // or we could add the two variables as:
 * $this->share(['variable'=>10.5,'variable2'=>'hello']);
 *
 *
 * @param string|array $var_name It is the name of the variable or it is an associative array
 * @param mixed        $value
 * @return self
 */
public function share( $var_name, $value = null ): self{}
```

Allows fore the creation of globals variable. This is best set in the Config class (detailed above) as you can pass in dependencies.

```
$provider->share('GLOBAL_foo', [$this->injected_dep, 'method']);
```

```

{{ $GLOBAL_foo }}
@include('some.path')
