Examples
Real-world code samples for WPZylos Views.
Admin Settings Page
Controller
class SettingsController
{
private ViewFactory $views;
public function index(): void
{
$settings = get_option('myplugin_settings', []);
echo $this->views->render('admin.settings', [
'settings' => $settings,
'tabs' => $this->getTabs(),
'activeTab' => $_GET['tab'] ?? 'general',
]);
}
}
View
<!-- resources/views/admin/settings.php -->
<div class="wrap">
<h1><?= esc_html__('Plugin Settings', 'myplugin') ?></h1>
<nav class="nav-tab-wrapper">
<?php foreach ($tabs as $tab): ?>
<a href="?page=myplugin-settings&tab=<?= esc_attr($tab['slug']) ?>"
class="nav-tab <?= $activeTab === $tab['slug'] ? 'nav-tab-active' : '' ?>">
<?= esc_html($tab['label']) ?>
</a>
<?php endforeach; ?>
</nav>
<form method="post" action="options.php">
<?php settings_fields('myplugin_settings'); ?>
<?= $this->views->render("admin.settings.{$activeTab}", compact('settings')) ?>
<?php submit_button(); ?>
</form>
</div>
Product List
Controller
class ProductController
{
private ViewFactory $views;
public function index(): void
{
$products = $this->productService->paginate(
page: (int) ($_GET['paged'] ?? 1),
perPage: 20
);
echo $this->views->render('admin.products.index', [
'products' => $products['data'],
'pagination' => $products['pagination'],
]);
}
}
View
<!-- resources/views/admin/products/index.php -->
<div class="wrap">
<h1 class="wp-heading-inline">
<?= esc_html__('Products', 'myplugin') ?>
</h1>
<a href="?page=products&action=new" class="page-title-action">
<?= esc_html__('Add New', 'myplugin') ?>
</a>
<hr class="wp-header-end">
<table class="wp-list-table widefat striped">
<thead>
<tr>
<th><?= esc_html__('Name', 'myplugin') ?></th>
<th><?= esc_html__('SKU', 'myplugin') ?></th>
<th><?= esc_html__('Price', 'myplugin') ?></th>
<th><?= esc_html__('Stock', 'myplugin') ?></th>
<th><?= esc_html__('Actions', 'myplugin') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $product): ?>
<?= $views->render('admin.products.row', compact('product')) ?>
<?php endforeach; ?>
</tbody>
</table>
<?= $views->render('partials.pagination', compact('pagination')) ?>
</div>
Email Template
<!-- resources/views/emails/order-confirmation.php -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body { font-family: Arial, sans-serif; }
.header { background: #333; color: white; padding: 20px; }
.content { padding: 20px; }
</style>
</head>
<body>
<div class="header">
<h1><?= esc_html($siteName) ?></h1>
</div>
<div class="content">
<h2><?= esc_html__('Order Confirmation', 'myplugin') ?></h2>
<p>
<?= sprintf(
esc_html__('Thank you for your order, %s!', 'myplugin'),
esc_html($customer->first_name)
) ?>
</p>
<table>
<?php foreach ($order->items as $item): ?>
<tr>
<td><?= esc_html($item->name) ?></td>
<td><?= esc_html($item->quantity) ?></td>
<td><?= wc_price($item->total) ?></td>
</tr>
<?php endforeach; ?>
</table>
<p><strong>Total: <?= wc_price($order->total) ?></strong></p>
</div>
</body>
</html>