diff --git a/src/EnvironmentFactory.php b/src/EnvironmentFactory.php index 6c8cae4..5e2f18a 100644 --- a/src/EnvironmentFactory.php +++ b/src/EnvironmentFactory.php @@ -87,9 +87,7 @@ public function setResourceLimits(ResourceLimits $resourceLimits): EnvironmentFa public function setRethrowErrors(bool $rethrowErrors = true): EnvironmentFactory { - $this->defaultRenderContextOptions = new RenderContextOptions( - strictVariables: $this->defaultRenderContextOptions->strictVariables, - strictFilters: $this->defaultRenderContextOptions->strictFilters, + $this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith( rethrowErrors: $rethrowErrors, ); @@ -98,10 +96,8 @@ public function setRethrowErrors(bool $rethrowErrors = true): EnvironmentFactory public function setStrictVariables(bool $strictVariables = true): EnvironmentFactory { - $this->defaultRenderContextOptions = new RenderContextOptions( + $this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith( strictVariables: $strictVariables, - strictFilters: $this->defaultRenderContextOptions->strictFilters, - rethrowErrors: $this->defaultRenderContextOptions->rethrowErrors, ); return $this; @@ -109,10 +105,17 @@ public function setStrictVariables(bool $strictVariables = true): EnvironmentFac public function setStrictFilters(bool $strictFilters = true): EnvironmentFactory { - $this->defaultRenderContextOptions = new RenderContextOptions( - strictVariables: $this->defaultRenderContextOptions->strictVariables, + $this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith( strictFilters: $strictFilters, - rethrowErrors: $this->defaultRenderContextOptions->rethrowErrors, + ); + + return $this; + } + + public function setLazyParsing(bool $lazyParsing = true): EnvironmentFactory + { + $this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith( + lazyParsing: $lazyParsing, ); return $this; diff --git a/src/Render/RenderContext.php b/src/Render/RenderContext.php index c1660dd..b8ad86b 100644 --- a/src/Render/RenderContext.php +++ b/src/Render/RenderContext.php @@ -311,13 +311,13 @@ public function getTemplateName(): ?string return $this->templateName; } - public function loadPartial(string $templateName, bool $parseIfMissing = false): Template + public function loadPartial(string $templateName): Template { if ($partial = $this->environment->templatesCache->get($templateName)) { return $partial; } - if (! $parseIfMissing) { + if ($this->options->lazyParsing === false) { throw new StandardException(sprintf("The partial '%s' has not be loaded during parsing", $templateName)); } diff --git a/src/Render/RenderContextOptions.php b/src/Render/RenderContextOptions.php index ccc9d78..29d8535 100644 --- a/src/Render/RenderContextOptions.php +++ b/src/Render/RenderContextOptions.php @@ -17,5 +17,23 @@ public function __construct( * Rethrow exceptions that occur during rendering instead of rendering the error message. */ public readonly bool $rethrowErrors = false, + /** + * Allow parsing the template when it is rendered if it has not been parsed yet. + */ + public readonly bool $lazyParsing = true, ) {} + + public function cloneWith( + ?bool $strictVariables = null, + ?bool $strictFilters = null, + ?bool $rethrowErrors = null, + ?bool $lazyParsing = null, + ): RenderContextOptions { + return new RenderContextOptions( + strictVariables: $strictVariables ?? $this->strictVariables, + strictFilters: $strictFilters ?? $this->strictFilters, + rethrowErrors: $rethrowErrors ?? $this->rethrowErrors, + lazyParsing: $lazyParsing ?? $this->lazyParsing, + ); + } } diff --git a/src/Tags/RenderTag.php b/src/Tags/RenderTag.php index b6b4255..9ffa60e 100644 --- a/src/Tags/RenderTag.php +++ b/src/Tags/RenderTag.php @@ -159,7 +159,7 @@ protected function loadPartial(RenderContext $context): Template throw new SyntaxException('Template name must be a string'); } - return $context->loadPartial($templateName, parseIfMissing: $this->allowDynamicPartials()); + return $context->loadPartial($templateName); } protected function buildPartialContext(RenderContext $rootContext, string $templateName, array $variables = []): RenderContext