API Reference

ViewFactory

WPZylos\Framework\Views\ViewFactory

Resolves and renders views using registered template engines.

Constructor

public function __construct(ContextInterface $context, ?string $basePath = null)
ParameterTypeDefaultDescription
$contextContextInterface-Plugin context
$basePathstring or nullnullBase views path. Defaults to $context->path('resources/views')

The PhpEngine is registered automatically on construction.


render

public function render(string $view, array $data = []): string

Render a view template immediately and return the output string.

ParameterTypeDefaultDescription
$viewstring-View name in dot notation
$dataarray<string, mixed>[]Data to pass to the template

Shared data is merged with $data (per-view data takes precedence).

Returns: string - Rendered content.

Throws: InvalidArgumentException if the view file is not found.


make

public function make(string $view, array $data = []): View

Create a View instance for deferred rendering.

ParameterTypeDefaultDescription
$viewstring-View name in dot notation
$dataarray<string, mixed>[]Initial data to pass to the template

Shared data is merged into the View at creation time.

Returns: View


exists

public function exists(string $view): bool

Check if a view file exists.

ParameterTypeDescription
$viewstringView name in dot notation

Returns: bool - true if the view file is found.


share

public function share(string|array $key, mixed $value = null): static

Share data with all views rendered by this factory.

ParameterTypeDefaultDescription
$keystring or array<string,mixed>-Key name or array of key-value pairs
$valuemixednullValue (when $key is a string)

Returns: static (fluent)


addEngine

public function addEngine(EngineInterface $engine): static

Register a custom template engine. Custom engines are prepended to the engine list and checked before the default PhpEngine.

ParameterTypeDescription
$engineEngineInterfaceEngine instance

Returns: static (fluent)


getBasePath

public function getBasePath(): string

Get the base views directory path.

Returns: string


View

WPZylos\Framework\Views\View

Represents a renderable view with data. Created by ViewFactory::make().

Constructor

public function __construct(ViewFactory $factory, string $view, array $data = [])
ParameterTypeDefaultDescription
$factoryViewFactory-Factory instance
$viewstring-View name
$dataarray<string, mixed>[]Initial view data

with

public function with(string|array $key, mixed $value = null): static

Add data to the view.

ParameterTypeDefaultDescription
$keystring or array<string,mixed>-Key name or array of key-value pairs
$valuemixednullValue (when $key is a string)

Returns: static (fluent)


render

public function render(): string

Render the view by delegating to ViewFactory::render().

Returns: string - Rendered content.


__toString

public function __toString(): string

Convert the view to a string. Calls render() internally, allowing the View to be used with echo.

Returns: string


EngineInterface

WPZylos\Framework\Views\EngineInterface

Interface that all template engines must implement.

render

public function render(string $path, array $data = []): string

Render a template file.

ParameterTypeDefaultDescription
$pathstring-Absolute path to template
$dataarray<string, mixed>[]Template data

Returns: string - Rendered content.


canRender

public function canRender(string $path): bool

Check if this engine can render the given file.

ParameterTypeDescription
$pathstringFile path

Returns: bool


PhpEngine

WPZylos\Framework\Views\Engines\PhpEngine

Implements EngineInterface. Renders plain PHP templates using output buffering and extract() with EXTR_SKIP.

render

public function render(string $path, array $data = []): string

Render a PHP template. Data keys become local variables inside the template.

Throws: InvalidArgumentException if the file does not exist.

Returns: string


canRender

public function canRender(string $path): bool

Returns true for files ending with .php.

Returns: bool


TwigEngine

WPZylos\Framework\Views\Engines\TwigEngine

Implements EngineInterface. Wraps the Twig template engine with HTML autoescape enabled by default. Twig is an optional dependency (twig/twig: ^3.0).

Constructor

public function __construct(
    string $basePath,
    ?string $cachePath = null,
    bool $debug = false
)
ParameterTypeDefaultDescription
$basePathstring-View base directory path
$cachePathstring or nullnullCache directory (null = no cache)
$debugboolfalseEnable Twig debug mode

Twig environment is configured with:

  • autoescape: 'html'
  • strict_variables matches $debug

render

public function render(string $path, array $data = []): string

Render a Twig template. Converts the absolute path to a relative path for Twig's loader.

Returns: string


canRender

public function canRender(string $path): bool

Returns true for files ending with .twig or .html.twig.

Returns: bool


getEnvironment

public function getEnvironment(): \Twig\Environment

Get the underlying Twig environment for customization (adding globals, filters, functions, etc.).

Returns: \Twig\Environment


CssHelper

WPZylos\Framework\Views\CssHelper

CSS prefix helper for DaisyUI component classes. Generates prefixed class names for style isolation between plugins.

Constructor

public function __construct(ContextInterface $context)
ParameterTypeDescription
$contextContextInterfacePlugin context

The prefix is read from $context->config()->get('ui.css_prefix'). If not configured, it is derived from the first letter of each word in the plugin name (e.g., "First Class Dress Clothing""fcds-").


prefix

public function prefix(): string

Get the CSS prefix with trailing hyphen.

Returns: string — e.g., 'fcds-'


prefixClean

public function prefixClean(): string

Get the CSS prefix without trailing hyphen.

Returns: string — e.g., 'fcds'


cls

public function cls(string ...$classes): string

Generate prefixed class name(s).

ParameterTypeDescription
...$classesstring[]DaisyUI class names (without prefix)

Returns: string — Space-separated prefixed class names.

Example:

$css->cls('btn', 'btn-primary');
// Returns: 'fcds-btn fcds-btn-primary'

adminRootId

public function adminRootId(): string

Generate the admin root container ID.

Returns: string — e.g., 'fcds-admin'


adminOpen

public function adminOpen(string $extraClasses = 'wrap'): string

Generate an opening admin root div tag.

ParameterTypeDefaultDescription
$extraClassesstring'wrap'Additional CSS classes

Returns: string — HTML opening tag.


adminClose

public function adminClose(): string

Generate closing admin root div tag.

Returns: string'</div>'


JsMount

WPZylos\Framework\Views\JsMount

JavaScript framework mount helper. Renders mount points for Vue.js and React apps with WordPress data passing via window variables.

Constructor

public function __construct(ContextInterface $context, CssHelper $css)
ParameterTypeDescription
$contextContextInterfacePlugin context
$cssCssHelperCSS helper for admin scope

adminMount

public function adminMount(string $id, array $data = [], string $varName = ''): string

Render a Vue/React mount point wrapped in admin root scope.

ParameterTypeDefaultDescription
$idstring-Mount element ID
$dataarray<string, mixed>[]Data to pass via window variable
$varNamestring''JS global variable name (auto-derived if empty)

Returns: string — HTML with admin root wrapper, mount div, and inline script.


frontendMount

public function frontendMount(string $id, array $data = [], string $varName = ''): string

Render a frontend mount point (no admin root scope).

ParameterTypeDefaultDescription
$idstring-Mount element ID
$dataarray<string, mixed>[]Data to pass via window variable
$varNamestring''JS global variable name (auto-derived if empty)

Returns: string — HTML with mount div and optional inline script.


shortcodeMount

public function shortcodeMount(string $id, array $data = []): string

Render a shortcode mount point. Delegates to frontendMount().

ParameterTypeDefaultDescription
$idstring-Unique mount ID
$dataarray<string, mixed>[]Data to pass

Returns: string — HTML.


ViewsServiceProvider

WPZylos\Framework\Views\ViewsServiceProvider

Registers the ViewFactory, CssHelper, and JsMount with the application container.

register

public function register(ApplicationInterface $app): void

Registers the following container bindings:

BindingTypeDescription
ViewFactory::classSingletonViewFactory instance
'view'SingletonAlias to ViewFactory
CssHelper::classSingletonCssHelper instance
'css'SingletonAlias to CssHelper
JsMount::classSingletonJsMount instance
'js.mount'SingletonAlias to JsMount

The ViewFactory is created with:

  • Context from $app->context()
  • Base path from $app->paths()->path('@views')