Skip to content

TemplateLayerLoader

Twig loader that treats the template “name” Twig is given as a literal, already-resolved filesystem path.

Template resolution (directory conventions, locale fallback, extension) is entirely the TemplateLayer’s job — TwigRenderer always calls Twig with $layer->getResourceStreamIdentifier() as the name, so this loader never needs a base directory or its own lookup rules.

final class TemplateLayerLoader implements LoaderInterface

ImplementsLoaderInterface
SourceTemplateLayerLoader.php
MethodDescription
exists(string $name): boolReports whether the template path exists and is readable by this process.
getCacheKey(string $name): stringReturns the cache key for a template.
getSourceContext(string $name): SourceReads the template at $name and returns it as a Twig source.
isFresh(string $name, int $time): boolReports whether the compiled template cached at $time is still current.

public function exists(string $name): bool

Reports whether the template path exists and is readable by this process.

A file that exists but is not readable counts as missing, because TemplateLayerLoader::getSourceContext() could not load it either.

ParameterTypeDescription
$namestring

Returns bool

public function getCacheKey(string $name): string

Returns the cache key for a template.

The name is already a fully resolved absolute path, so it is unique on its own and is returned unchanged.

ParameterTypeDescription
$namestring

Returns string

public function getSourceContext(string $name): Source

Reads the template at $name and returns it as a Twig source.

$name is used verbatim as a filesystem path. Throws a LoaderError when the file cannot be read, since Twig has no other way to signal a missing template from here.

ParameterTypeDescription
$namestring

Returns Source

ThrowsWhen
LoaderError

public function isFresh(string $name, int $time): bool

Reports whether the compiled template cached at $time is still current.

Compares the template file’s modification time against $time. A file whose mtime cannot be read is treated as stale, so Twig recompiles it rather than serving a cache entry that may no longer match.

ParameterTypeDescription
$namestring
$timeint

Returns bool