Skip to content

Commit

Permalink
Remove $templateName from Template::loadTemplate()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 21, 2025
1 parent 9b4db2e commit 850117a
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/Node/EmbedNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ protected function addGetTemplate(Compiler $compiler, string $template = ''): vo
->raw('$this->loadTemplate(')
->string($this->getAttribute('name'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(', ')
->string($this->getAttribute('index'))
Expand Down
2 changes: 0 additions & 2 deletions src/Node/Expression/BlockReferenceExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ private function compileTemplateCall(Compiler $compiler, string $method): Compil
->write('$this->loadTemplate(')
->subcompile($this->getNode('template'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
Expand Down
2 changes: 0 additions & 2 deletions src/Node/ImportNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public function compile(Compiler $compiler): void
->raw('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')->unwrap()')
;
Expand Down
2 changes: 0 additions & 2 deletions src/Node/IncludeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ protected function addGetTemplate(Compiler $compiler/* , string $template = '' *
->raw('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
Expand Down
6 changes: 0 additions & 6 deletions src/Node/ModuleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ protected function compileGetParent(Compiler $compiler)
->raw('$this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($this->getSourceContext()->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(')')
;
Expand Down Expand Up @@ -221,8 +219,6 @@ protected function compileConstructor(Compiler $compiler)
->write(\sprintf('$_trait_%s = $this->loadTemplate(', $i))
->subcompile($node)
->raw(', ')
->repr($node->getTemplateName())
->raw(', ')
->repr($node->getTemplateLine())
->raw(");\n")
->write(\sprintf("if (!\$_trait_%s->unwrap()->isTraitable()) {\n", $i))
Expand Down Expand Up @@ -356,8 +352,6 @@ protected function compileDisplay(Compiler $compiler)
->write('$this->parent = $this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($this->getSourceContext()->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(");\n")
;
Expand Down
4 changes: 2 additions & 2 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function getBlockNames(array $context, array $blocks = []): array
/**
* @param string|TemplateWrapper|array<string|TemplateWrapper> $template
*/
protected function loadTemplate($template, $templateName = null, $line = null, $index = null): self|TemplateWrapper
protected function loadTemplate($template, int|null $line = null, int|null $index = null): self|TemplateWrapper
{
try {
if (\is_array($template)) {
Expand Down Expand Up @@ -299,7 +299,7 @@ protected function loadTemplate($template, $templateName = null, $line = null, $
return $this->env->loadTemplate($class, $template, $index);
} catch (Error $e) {
if (!$e->getSourceContext()) {
$e->setSourceContext($templateName ? new Source('', $templateName) : $this->getSourceContext());
$e->setSourceContext($this->getSourceContext());
}

if ($e->getTemplateLine() > 0) {
Expand Down
65 changes: 65 additions & 0 deletions tests/Node/ExtendsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Twig\Tests\Node;

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Test\NodeTestCase;

class ExtendsTest extends NodeTestCase
{
public function testErrorFromArrayLoader()
{
$twig = new Environment(new ArrayLoader([
'index.twig' => '{% include "include.twig" %}',
'include.twig' => $include = <<<EOF
{% extends 'invalid.twig' %}
EOF,
]), ['debug' => true]);
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertSame('Template "invalid.twig" is not defined.', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}

public function testErrorFromFilesystemLoader()
{
$twig = new Environment(new FilesystemLoader([
$dir = dirname(__DIR__, 2).'/_templates',
]), ['debug' => true]);
$include = file_get_contents($dir.'/include.twig');
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Unable to find template "invalid.twig"', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}

public static function provideTests(): iterable
{
return [];
}
}
2 changes: 1 addition & 1 deletion tests/Node/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function provideTests(): iterable

$tests[] = [$node, <<<EOF
// line 1
\$macros["macro"] = \$this->macros["macro"] = \$this->loadTemplate("foo.twig", null, 1)->unwrap();
\$macros["macro"] = \$this->macros["macro"] = \$this->loadTemplate("foo.twig", 1)->unwrap();
EOF
];

Expand Down
10 changes: 5 additions & 5 deletions tests/Node/IncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function provideTests(): iterable
$node = new IncludeNode($expr, null, false, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate("foo.twig", null, 1)->unwrap()->yield($context);
yield from $this->loadTemplate("foo.twig", 1)->unwrap()->yield($context);
EOF
];

Expand All @@ -55,7 +55,7 @@ public static function provideTests(): iterable
$node = new IncludeNode($expr, null, false, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate(((true) ? ("foo") : ("foo")), null, 1)->unwrap()->yield($context);
yield from $this->loadTemplate(((true) ? ("foo") : ("foo")), 1)->unwrap()->yield($context);
EOF
];

Expand All @@ -64,22 +64,22 @@ public static function provideTests(): iterable
$node = new IncludeNode($expr, $vars, false, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate("foo.twig", null, 1)->unwrap()->yield(CoreExtension::merge($context, ["foo" => true]));
yield from $this->loadTemplate("foo.twig", 1)->unwrap()->yield(CoreExtension::merge($context, ["foo" => true]));
EOF
];

$node = new IncludeNode($expr, $vars, true, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate("foo.twig", null, 1)->unwrap()->yield(CoreExtension::toArray(["foo" => true]));
yield from $this->loadTemplate("foo.twig", 1)->unwrap()->yield(CoreExtension::toArray(["foo" => true]));
EOF
];

$node = new IncludeNode($expr, $vars, true, true, 1);
$tests[] = [$node, <<<EOF
// line 1
try {
\$_v%s = \$this->loadTemplate("foo.twig", null, 1);
\$_v%s = \$this->loadTemplate("foo.twig", 1);
} catch (LoaderError \$e) {
// ignore missing template
\$_v%s = null;
Expand Down
6 changes: 3 additions & 3 deletions tests/Node/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ protected function doDisplay(array \$context, array \$blocks = []): iterable
{
\$macros = \$this->macros;
// line 2
\$macros["macro"] = \$this->macros["macro"] = \$this->loadTemplate("foo.twig", "foo.twig", 2)->unwrap();
\$macros["macro"] = \$this->macros["macro"] = \$this->loadTemplate("foo.twig", 2)->unwrap();
// line 1
\$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
\$this->parent = \$this->loadTemplate("layout.twig", 1);
yield from \$this->parent->unwrap()->yield(\$context, array_merge(\$this->blocks, \$blocks));
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public function __construct(Environment \$env)
protected function doGetParent(array \$context): bool|string|Template|TemplateWrapper
{
// line 2
return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2);
return \$this->loadTemplate(((true) ? ("foo") : ("foo")), 2);
}
protected function doDisplay(array \$context, array \$blocks = []): iterable
Expand Down

0 comments on commit 850117a

Please sign in to comment.