This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Server components for Symfony.

UI server componentsComments
Scroll

PHP logic and Twig markup in one .phem file.

src/Ui/LikeButton.phem
#[Component]
class LikeButton
{
    #[State]
    public int $likes = 0;

    public function like(): void
    {
        ++$this->likes;
    }

    public function render(): Node
    {
        return (
            <button onClick={{ like }}>
                Like · {{ likes }}
            </button>
        );
    }
}

The handler runs on your server and sends back only the HTML that changed.

onClickEventHTML
PHPlike()

Components nest, and a page is a component with a URL.

Document
ArticleUI server components
src/Ui/BlogPostPage.phem
#[Page('/posts/{id:post}')]
function BlogPostPage(Post $post): Node
{
    return (
        <Document title={{ post.title }}>
            <Article title={{ post.title }}>
                {{ post.content }}
                <LikeButton post={{ post }} />
            </Article>
        </Document>
    );
}

Services, security and rate limits work as they do in a controller.

#[Broadcast] sends shared data to every viewer through Mercure, and the UI state stays in each browser.

UI server componentsComments
UI server componentsComments
UI server componentsComments
Mercure
src/Ui/LikeButton.phem
#[Component, Broadcast]
class LikeButton
{
    #[Prop, BroadcastKey] public Post $post;

    #[Transactional]
    public function like(): void
    {
        ++$this->post->likes;
    }
}
$post->likes
Shared data: every viewer
#[Transactional]
Saved in one transaction: no save() call

phemc compiles each .phem file to plain PHP and Twig at build time.

LikeButton.phemphemc
PHP
var/phem/php/Ui/LikeButton.php
public function render(): Node
{
    return (
        new \Phem\Element('Ui/LikeButton.0.twig', [
            'like' => $this->like(...),
            'likes' => $this->likes,
        ])
    );
}
Twig
var/phem/templates/Ui/LikeButton.0.twig
<button {{ phem_on('click', like) }}>
    Like · {{ likes }}
</button>

$ composer require phem/phemGet startedExplore the docs
Phemphp

Start here

Phem
Esc
↑↓ to navigate↵ to openEsc to close