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;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ use App\Liquid\Filters\Numbers;
|
|||
use App\Liquid\Filters\StandardFilters;
|
||||
use App\Liquid\Filters\StringMarkup;
|
||||
use App\Liquid\Filters\Uniqueness;
|
||||
use App\Liquid\Tags\PluginRenderTag;
|
||||
use App\Liquid\Tags\TemplateTag;
|
||||
use App\Services\Plugin\Parsers\ResponseParserRegistry;
|
||||
use App\Services\PluginImportService;
|
||||
|
|
@ -499,6 +500,8 @@ class Plugin extends Model
|
|||
|
||||
// Register the template tag for inline templates
|
||||
$environment->tagRegistry->register(TemplateTag::class);
|
||||
// Use plugin render tag so partials receive trmnl, size, data, config
|
||||
$environment->tagRegistry->register(PluginRenderTag::class);
|
||||
|
||||
// Apply Liquid replacements (including 'with' syntax conversion)
|
||||
$processedMarkup = $this->applyLiquidReplacements($markup);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue