diff --git a/app/Liquid/Tags/PluginRenderTag.php b/app/Liquid/Tags/PluginRenderTag.php new file mode 100644 index 0000000..e4c441a --- /dev/null +++ b/app/Liquid/Tags/PluginRenderTag.php @@ -0,0 +1,45 @@ + + */ + 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; + } +} diff --git a/app/Models/Plugin.php b/app/Models/Plugin.php index 5eeeb6b..d377956 100644 --- a/app/Models/Plugin.php +++ b/app/Models/Plugin.php @@ -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); diff --git a/tests/Feature/PluginInlineTemplatesTest.php b/tests/Feature/PluginInlineTemplatesTest.php index 76b29d7..0bd5963 100644 --- a/tests/Feature/PluginInlineTemplatesTest.php +++ b/tests/Feature/PluginInlineTemplatesTest.php @@ -1,6 +1,7 @@ assertStringContainsString('"35":[{"name":"Ryan","age":35}]', $result); $this->assertStringContainsString('"29":[{"name":"Sara","age":29},{"name":"Jimbob","age":29}]', $result); }); + +test('shared template receives trmnl context when', function (): void { + $user = User::factory()->create(['name' => 'Jane Smith']); + + $plugin = Plugin::factory()->create([ + 'user_id' => $user->id, + 'name' => 'Departures', + 'markup_language' => 'liquid', + 'render_markup_shared' => <<<'LIQUID' +{% template departures_view %} +
+{% endtemplate %} +LIQUID + , + 'render_markup' => <<<'LIQUID' +