mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-03-14 12:23:33 +00:00
fix: shared template context injection
This commit is contained in:
parent
35ca55a90b
commit
a37a9cfe96
3 changed files with 80 additions and 0 deletions
45
app/Liquid/Tags/PluginRenderTag.php
Normal file
45
app/Liquid/Tags/PluginRenderTag.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Liquid\Tags;
|
||||
|
||||
use Keepsuit\Liquid\Render\RenderContext;
|
||||
use Keepsuit\Liquid\Support\MissingValue;
|
||||
use Keepsuit\Liquid\Tags\RenderTag;
|
||||
|
||||
/**
|
||||
* Render tag that injects plugin context (trmnl, size, data, config) into partials
|
||||
* so shared templates can use variables like trmnl.user.name without passing them explicitly.
|
||||
*/
|
||||
class PluginRenderTag extends RenderTag
|
||||
{
|
||||
/**
|
||||
* Root-level keys from the plugin render context that should be available in partials.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
private const PARENT_CONTEXT_KEYS = ['trmnl', 'size', 'data', 'config'];
|
||||
|
||||
protected function buildPartialContext(RenderContext $rootContext, string $templateName, array $variables = []): RenderContext
|
||||
{
|
||||
$partialContext = $rootContext->newIsolatedSubContext($templateName);
|
||||
|
||||
foreach (self::PARENT_CONTEXT_KEYS as $key) {
|
||||
$value = $rootContext->get($key);
|
||||
if ($value !== null && ! $value instanceof MissingValue) {
|
||||
$partialContext->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($variables as $key => $value) {
|
||||
$partialContext->set($key, $value);
|
||||
}
|
||||
|
||||
foreach ($this->attributes as $key => $value) {
|
||||
$partialContext->set($key, $rootContext->evaluate($value));
|
||||
}
|
||||
|
||||
return $partialContext;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue