Skip to content

Plugin System

Ray Fung edited this page Feb 26, 2026 · 3 revisions

Plugin System

Razy uses a shared PluginTrait to add plugin loading capabilities to core classes. Plugins extend the functionality of Template, Collection, Statement, and other components through lazy-loaded PHP closures.

PluginTrait

Classes that use PluginTrait gain the loadPlugin() method, which searches a structured plugin directory to load closures on demand.

use Razy\PluginTrait;



class MyComponent {

    use PluginTrait;



    public function doSomething() {

        $plugin = $this->loadPlugin('validator', 'email');

        if ($plugin) {

            return $plugin($data);

        }

    }

}

Classes Using Plugins

| Class | Plugin Types | Location |

| --- | --- | --- |

| Template | Modifiers, Functions, Custom Functions | src/plugins/template/ |

| Statement | Builder plugins | src/plugins/statement/ |

| Collection | Filter plugins | src/plugins/collection/ |

Template Plugins

Modifiers

Template modifiers transform output in the rendering pipeline. They are invoked with the pipe | operator in template tags.


src/plugins/template/modifier/

→?→ upper.php

→?→ lower.php

→?→ escape.php

→?→ trim.php

→?→ nl2br.php

→?→ json.php

→?→ base64.php

→?→ md5.php

→?→ number_format.php

→?→ date.php

→?→ default.php

→?→ truncate.php

→?→ url_encode.php

A modifier plugin file returns a closure:

// modifier/upper.php

return function($value): string {

    return strtoupper((string) $value);

};

Function Tags

Function plugins provide control flow and utility within templates. Called using {@functionName ...} syntax.


src/plugins/template/function/

→?→ if.php

→?→ include.php

→?→ url.php

→?→ asset.php

Statement Builder Plugins

Builder plugins extend the Statement query builder with named sub-interfaces:

$builder = $statement->builder('search', $searchTerm);

// Adds search-specific WHERE conditions

Collection Filter Plugins

Filter plugins are invoked through the selector syntax:

// filter/greaterThan.php

return function($value, $threshold): bool {

    return $value > (int) $threshold;

};



// Usage: $collection('items.*:greaterThan(price, 100)')

Global Plugin Directory

Shared plugins are loaded from plugins/ directories in the workspace. Module-specific plugins go in the module's own plugins/ folder:


playground/plugins/

→?→ template/

→  →?→ modifier/

→  →?→ function/

→?→ statement/

→  →?→ builder/

→?→ collection/

    →?→ filter/

Creating Custom Plugins

  1. Identify the plugin type (modifier, function, builder, filter)

  2. Create a PHP file in the appropriate directory

  3. The file must return a Closure

  4. The filename becomes the plugin name (without .php)

💡 Tip: Plugin files are lazy-loaded — they are only included when first referenced, keeping memory usage minimal.

← PreviousDOM Builder

Next → CLI & Terminal

Clone this wiki locally