function Environment::loadTemplate
Loads a template internal representation.
This method is for internal use only and should never be called directly.
@internal
Parameters
string $name The template name:
int|null $index The index if it is an embedded template:
Throws
LoaderError When the template cannot be found
RuntimeError When a previously generated cache is corrupted
SyntaxError When an error occurred during compilation
2 calls to Environment::loadTemplate()
- Environment::createTemplate in vendor/
twig/ twig/ src/ Environment.php - Creates a template from source.
- Environment::load in vendor/
twig/ twig/ src/ Environment.php - Loads a template.
File
-
vendor/
twig/ twig/ src/ Environment.php, line 375
Class
- Environment
- Stores the Twig configuration and renders templates.
Namespace
TwigCode
public function loadTemplate(string $cls, string $name, ?int $index = null) : Template {
$mainCls = $cls;
if (null !== $index) {
$cls .= '___' . $index;
}
if (isset($this->loadedTemplates[$cls])) {
return $this->loadedTemplates[$cls];
}
if (!class_exists($cls, false)) {
$key = $this->cache
->generateKey($name, $mainCls);
if (!$this->isAutoReload() || $this->isTemplateFresh($name, $this->cache
->getTimestamp($key))) {
$this->cache
->load($key);
}
if (!class_exists($cls, false)) {
$source = $this->getLoader()
->getSourceContext($name);
$content = $this->compileSource($source);
if (!isset($this->hotCache[$name])) {
$this->cache
->write($key, $content);
$this->cache
->load($key);
}
if (!class_exists($mainCls, false)) {
/* Last line of defense if either $this->bcWriteCacheFile was used,
* $this->cache is implemented as a no-op or we have a race condition
* where the cache was cleared between the above calls to write to and load from
* the cache.
*/
eval('?>' . $content);
}
if (!class_exists($cls, false)) {
throw new RuntimeError(\sprintf('Failed to load Twig template "%s", index "%s": cache might be corrupted.', $name, $index), -1, $source);
}
}
}
$this->extensionSet
->initRuntime();
return $this->loadedTemplates[$cls] = new $cls($this);
}