Skip to content

Commit c8db65f

Browse files
authored
Merge pull request #39 from keepsuit/lazy-parsing-option
lazy template parsing
2 parents b16fc90 + c2f2aad commit c8db65f

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

src/EnvironmentFactory.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ public function setResourceLimits(ResourceLimits $resourceLimits): EnvironmentFa
8787

8888
public function setRethrowErrors(bool $rethrowErrors = true): EnvironmentFactory
8989
{
90-
$this->defaultRenderContextOptions = new RenderContextOptions(
91-
strictVariables: $this->defaultRenderContextOptions->strictVariables,
92-
strictFilters: $this->defaultRenderContextOptions->strictFilters,
90+
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
9391
rethrowErrors: $rethrowErrors,
9492
);
9593

@@ -98,21 +96,26 @@ public function setRethrowErrors(bool $rethrowErrors = true): EnvironmentFactory
9896

9997
public function setStrictVariables(bool $strictVariables = true): EnvironmentFactory
10098
{
101-
$this->defaultRenderContextOptions = new RenderContextOptions(
99+
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
102100
strictVariables: $strictVariables,
103-
strictFilters: $this->defaultRenderContextOptions->strictFilters,
104-
rethrowErrors: $this->defaultRenderContextOptions->rethrowErrors,
105101
);
106102

107103
return $this;
108104
}
109105

110106
public function setStrictFilters(bool $strictFilters = true): EnvironmentFactory
111107
{
112-
$this->defaultRenderContextOptions = new RenderContextOptions(
113-
strictVariables: $this->defaultRenderContextOptions->strictVariables,
108+
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
114109
strictFilters: $strictFilters,
115-
rethrowErrors: $this->defaultRenderContextOptions->rethrowErrors,
110+
);
111+
112+
return $this;
113+
}
114+
115+
public function setLazyParsing(bool $lazyParsing = true): EnvironmentFactory
116+
{
117+
$this->defaultRenderContextOptions = $this->defaultRenderContextOptions->cloneWith(
118+
lazyParsing: $lazyParsing,
116119
);
117120

118121
return $this;

src/Render/RenderContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,13 @@ public function getTemplateName(): ?string
311311
return $this->templateName;
312312
}
313313

314-
public function loadPartial(string $templateName, bool $parseIfMissing = false): Template
314+
public function loadPartial(string $templateName): Template
315315
{
316316
if ($partial = $this->environment->templatesCache->get($templateName)) {
317317
return $partial;
318318
}
319319

320-
if (! $parseIfMissing) {
320+
if ($this->options->lazyParsing === false) {
321321
throw new StandardException(sprintf("The partial '%s' has not be loaded during parsing", $templateName));
322322
}
323323

src/Render/RenderContextOptions.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,23 @@ public function __construct(
1717
* Rethrow exceptions that occur during rendering instead of rendering the error message.
1818
*/
1919
public readonly bool $rethrowErrors = false,
20+
/**
21+
* Allow parsing the template when it is rendered if it has not been parsed yet.
22+
*/
23+
public readonly bool $lazyParsing = true,
2024
) {}
25+
26+
public function cloneWith(
27+
?bool $strictVariables = null,
28+
?bool $strictFilters = null,
29+
?bool $rethrowErrors = null,
30+
?bool $lazyParsing = null,
31+
): RenderContextOptions {
32+
return new RenderContextOptions(
33+
strictVariables: $strictVariables ?? $this->strictVariables,
34+
strictFilters: $strictFilters ?? $this->strictFilters,
35+
rethrowErrors: $rethrowErrors ?? $this->rethrowErrors,
36+
lazyParsing: $lazyParsing ?? $this->lazyParsing,
37+
);
38+
}
2139
}

src/Tags/RenderTag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function loadPartial(RenderContext $context): Template
159159
throw new SyntaxException('Template name must be a string');
160160
}
161161

162-
return $context->loadPartial($templateName, parseIfMissing: $this->allowDynamicPartials());
162+
return $context->loadPartial($templateName);
163163
}
164164

165165
protected function buildPartialContext(RenderContext $rootContext, string $templateName, array $variables = []): RenderContext

0 commit comments

Comments
 (0)