PHP code example of plixus / twig-component-preview-bundle
1. Go to this page and download the library: Download plixus/twig-component-preview-bundle 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/ */
plixus / twig-component-preview-bundle example snippets
namespace App\Twig\Components;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Plixus\TwigComponentPreviewBundle\Attribute\PreviewableComponent;
use Plixus\TwigComponentPreviewBundle\Attribute\PreviewProperty;
#[AsTwigComponent] // ← Normal Twig Component
#[PreviewableComponent( // ← Add this for preview
name: 'Alert Component',
description: 'Displays contextual feedback messages'
)]
class Alert
{
#[PreviewProperty(
type: 'choice',
label: 'Alert Type',
choices: ['info', 'success', 'warning', 'danger'],
default: 'info',
help: 'The visual style and semantic meaning'
)]
public string $type = 'info';
#[PreviewProperty(
type: 'checkbox',
label: 'Dismissible',
default: false,
help: 'Whether the alert can be closed by the user'
)]
public bool $dismissible = false;
#[PreviewProperty(
type: 'textarea',
label: 'Message',
default: 'This is an alert message',
namespace App\Controller;
use App\Twig\Components\Alert;
use App\Twig\Components\Button;
use App\Twig\Components\Card;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ComponentPreviewController extends AbstractController
{
#[Route('/preview/{component}', name: 'component_preview')]
public function preview(string $component): Response
{
// Manual mapping - you define which components are previewable
$componentClasses = [
'alert' => Alert::class,
'button' => Button::class,
'card' => Card::class,
// Add your components here
];
if (!isset($componentClasses[$component])) {
throw $this->createNotFoundException("Component '{$component}' not found");
}
return $this->render('preview/component.html.twig', [
'component_class' => $componentClasses[$component],
'component_name' => ucfirst($component)
]);
}
}
class MyComponent
{
#[PreviewProperty(type: 'text', label: 'Name', default: 'John')]
public string $name;
#[PreviewProperty(type: 'email', label: 'Email Address')]
public string $email;
#[PreviewProperty(type: 'textarea', label: 'Description', formOptions: ['attr' => ['rows' => 4]])]
public string $description;
#[PreviewProperty(type: 'choice', choices: ['small', 'medium', 'large'], default: 'medium')]
public string $size;
#[PreviewProperty(type: 'checkbox', label: 'Is Active', default: true)]
public bool $active;
#[PreviewProperty(type: 'number', label: 'Count', default: 1)]
public int $count;
#[PreviewProperty(type: 'url', label: 'Website')]
public ?string $website = null; // Nullable properties supported
}
#[PreviewProperty(
type: 'choice', // Form field type
label: 'Display Name', // Form label
help: 'Additional help text', // Help text below field
choices: ['a', 'b', 'c'], // For choice fields
default: 'a', // Default value