API Reference
ViewFactory
WPZylos\Framework\Views\ViewFactory
Resolves and renders views using registered template engines.
Constructor
public function __construct(ContextInterface $context, ?string $basePath = null)
| Parameter | Type | Default | Description |
|---|---|---|---|
$context | ContextInterface | - | Plugin context |
$basePath | string or null | null | Base 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$view | string | - | View name in dot notation |
$data | array<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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$view | string | - | View name in dot notation |
$data | array<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.
| Parameter | Type | Description |
|---|---|---|
$view | string | View 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$key | string or array<string,mixed> | - | Key name or array of key-value pairs |
$value | mixed | null | Value (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.
| Parameter | Type | Description |
|---|---|---|
$engine | EngineInterface | Engine 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 = [])
| Parameter | Type | Default | Description |
|---|---|---|---|
$factory | ViewFactory | - | Factory instance |
$view | string | - | View name |
$data | array<string, mixed> | [] | Initial view data |
with
public function with(string|array $key, mixed $value = null): static
Add data to the view.
| Parameter | Type | Default | Description |
|---|---|---|---|
$key | string or array<string,mixed> | - | Key name or array of key-value pairs |
$value | mixed | null | Value (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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$path | string | - | Absolute path to template |
$data | array<string, mixed> | [] | Template data |
Returns: string - Rendered content.
canRender
public function canRender(string $path): bool
Check if this engine can render the given file.
| Parameter | Type | Description |
|---|---|---|
$path | string | File 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
)
| Parameter | Type | Default | Description |
|---|---|---|---|
$basePath | string | - | View base directory path |
$cachePath | string or null | null | Cache directory (null = no cache) |
$debug | bool | false | Enable Twig debug mode |
Twig environment is configured with:
autoescape: 'html'strict_variablesmatches$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)
| Parameter | Type | Description |
|---|---|---|
$context | ContextInterface | Plugin 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).
| Parameter | Type | Description |
|---|---|---|
...$classes | string[] | 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$extraClasses | string | '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)
| Parameter | Type | Description |
|---|---|---|
$context | ContextInterface | Plugin context |
$css | CssHelper | CSS 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
$id | string | - | Mount element ID |
$data | array<string, mixed> | [] | Data to pass via window variable |
$varName | string | '' | 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).
| Parameter | Type | Default | Description |
|---|---|---|---|
$id | string | - | Mount element ID |
$data | array<string, mixed> | [] | Data to pass via window variable |
$varName | string | '' | 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().
| Parameter | Type | Default | Description |
|---|---|---|---|
$id | string | - | Unique mount ID |
$data | array<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:
| Binding | Type | Description |
|---|---|---|
ViewFactory::class | Singleton | ViewFactory instance |
'view' | Singleton | Alias to ViewFactory |
CssHelper::class | Singleton | CssHelper instance |
'css' | Singleton | Alias to CssHelper |
JsMount::class | Singleton | JsMount instance |
'js.mount' | Singleton | Alias to JsMount |
The ViewFactory is created with:
- Context from
$app->context() - Base path from
$app->paths()->path('@views')