Skip to content

Commit

Permalink
Merge branch 'b-7.1.x' into b-8.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
liulka-oxid committed Jan 11, 2024
2 parents a32da5f + 16a8274 commit f8f170e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
51 changes: 7 additions & 44 deletions src/TwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ public function __construct(
}
}

/**
* Renders a template.
*
* @param string $name A template name
* @param array $context An array of parameters to pass to the template
*
* @return string The evaluated template as a string
*
* @throws \RuntimeException if the template cannot be rendered
*/
public function render(string $name, array $context = []): string
{
return $this->engine->render(
Expand All @@ -56,62 +46,35 @@ public function render(string $name, array $context = []): string
);
}

/**
* Returns true if the template exists.
*
* @param string $name A template name
*
* @return bool true if the template exists, false otherwise
*
* @throws \RuntimeException if the engine cannot handle the template name
*/
public function exists(string $name): bool
{
return $this->engine->getLoader()->exists($name . '.' . $this->fileExtension);
if (!str_ends_with($name, $this->fileExtension)) {
$name .= ".$this->fileExtension";
}
return $this->engine->getLoader()->exists($name);
}

/**
* Renders a fragment of the template.
*
* @param string $fragment The template fragment to render
* @param string $fragmentId The Id of the fragment
* @param array $context An array of parameters to pass to the template
*
* @return string
*/
public function renderFragment(string $fragment, string $fragmentId, array $context = []): string
{
return $this->engine
->createTemplate($fragment, $fragmentId)
->render($context);
}

/**
* @param string $name
* @param mixed $value
*/
public function addGlobal(string $name, $value)
{
$this->engine->addGlobal($name, $value);
}

/**
* Returns assigned globals.
*
* @return array
*/
public function getGlobals(): array
{
return $this->engine->getGlobals();
}

/**
* @param EscaperInterface $escaper
*/
public function addEscaper(EscaperInterface $escaper)
{
/** @var EscaperExtension $escaperExtension */
$escaperExtension = $this->engine->getExtension(EscaperExtension::class);
$escaperExtension->setEscaper($escaper->getStrategy(), [$escaper, 'escape']);
$this->engine
->getExtension(EscaperExtension::class)
->setEscaper($escaper->getStrategy(), [$escaper, 'escape']);
}
}
11 changes: 10 additions & 1 deletion tests/Integration/TwigEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public function testExists(): void
$this->assertFalse($engine->exists('foo'));
}

public function testExistsWithFileExtension(): void
{
$this->assertTrue(
$this
->getEngine()
->exists("$this->template.$this->extension")
);
}

public function testAddGlobal(): void
{
$engine = $this->getEngine();
Expand Down Expand Up @@ -101,7 +110,7 @@ private function getEngine(): TwigEngine

private function getTemplateName(): string
{
return "{$this->template}.{$this->extension}";
return "$this->template.$this->extension";
}

private function getTemplateDir(): string
Expand Down

0 comments on commit f8f170e

Please sign in to comment.