Get support for sroze/live-twig
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
sroze/live-twig
LiveTwig
Let's get rid of the SPAs! Here's how to get real-time Twig blocks basically. Bisous. 😘
Setup
-
Install the dependency
composer req sroze/live-twig
-
Get Mercure Hub running. You can have a look to the Mercure documentation. Simplest is with Docker:
docker run --rm -e CORS_ALLOWED_ORIGINS='https://127.0.0.1:8000' -e JWT_KEY='!ChangeMe!' -e DEMO=1 -e ALLOW_ANONYMOUS=1 -e PUBLISH_ALLOWED_ORIGINS='http://localhost,http://localhost:8000' -p 80:80 dunglas/mercure
-
Get your Mercure JWT token. If you are using the default demo
JWT_KEY
, you can get the token from your running hub's homepage.. -
Set environment variables
.env
...
MERCURE_PUBLISH_URL=http://localhost/.well-known/mercure MERCURE_JWT_TOKEN=[your-token]
-
(While the Flex recipe is not done) Regisyer the bundle:
// config/bundles.php return [ // ... Symfony\Bundle\LiveTwigBundle\LiveTwigBundle::class => ['all' => true], ];
Create the following configuration file:
# config/packages/live_twig.yaml live_twig: mercure_public_url: "%env(MERCURE_PUBLISH_URL)%"
Use it!
Anywhere in your Twig templates, render a live block:
{{
render_live(
controller('App\\Controller\\YourController::yourAction', { some: 'parameter' }),
{'topics': ['foo', 'bar-' ~ baz]}
)
}}
When the data contained in this block change, publish an empty Mercure update:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mercure\Publisher;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Annotation\Route;
class LiveController extends AbstractController
{
/**
* @Route("/foo")
*/
public function index(Publisher $publisher)
{
// ...
$publisher(new Update(['foo', 'bar-'.$bar]));
// ...
}
}
When the browser will receive the signal, it will send a GET request to retrieve the new version of the block. Alternatively, you can also pass the data to display as the content of the update. In this case no extra HTTP request is triggered:
// ...
$publisher(new Update(['foo', 'bar-'.$bar], 'the updated content of the block'));
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2