1. Go to this page and download the library: Download vaibhavpandeyvpz/phew library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vaibhavpandeyvpz / phew example snippets
use Phew\View;
// Create a view instance
$view = new View();
// Add template folder
$view->add(__DIR__ . '/templates');
// Render a template
echo $view->fetch('welcome.php', ['name' => 'World']);
Hello, <?= $name
use Phew\View;
$view = new View();
$view->add(__DIR__ . '/templates');
// Set variables using array syntax
$view['title'] = 'My Page';
$view['user'] = ['name' => 'John', 'email' => '[email protected]'];
// Variables are available in all templates
echo $view->fetch('page.php');
$view = new View();
// Add folders with namespaces
$view->add(__DIR__ . '/admin/templates', 'admin');
$view->add(__DIR__ . '/public/templates', 'public');
// Use namespace when fetching
echo $view->fetch('admin:dashboard.php');
echo $view->fetch('public:home.php');
$view = new View();
$view->add(__DIR__ . '/templates');
// Register a function
$view->register('uppercase', fn(string $str) => strtoupper($str));
$view->register('formatDate', function($date) {
return date('Y-m-d', strtotime($date));
});
<p><?= $this->uppercase('hello world')
$userInput = '<script>alert("XSS")</script>';
use Phew\ExtensionInterface;
use Phew\ViewInterface;
class MyExtension implements ExtensionInterface
{
public function extend(ViewInterface $view): void
{
$view->register('myFunction', fn() => 'Hello from extension!');
}
}
$view = new View();
$view->install(new MyExtension());
use Phew\View;
use Phew\ViewException;
try {
$view = new View();
echo $view->fetch('nonexistent.php');
} catch (ViewException $e) {
echo 'Template error: ' . $e->getMessage();
}