Skip to content

Commit

Permalink
Add twig function to render original page title
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 authored and VincentLanglet committed Sep 25, 2021
1 parent 689055d commit dff934e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/reference/twig_helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ This will render the page title as follows:

<title>My custom title</title>

Render the page title text
^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: twig
{{ sonata_seo_title_text() }}
This will render the page title as follows:

.. code-block:: html

My custom title

Render page metadata
^^^^^^^^^^^^^^^^^^^^

Expand Down
6 changes: 6 additions & 0 deletions src/Twig/Extension/SeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function getFunctions()
{
return [
new TwigFunction('sonata_seo_title', [$this, 'getTitle'], ['is_safe' => ['html']]),
new TwigFunction('sonata_seo_title_text', [$this, 'getTitleText'], ['is_safe' => ['html']]),
new TwigFunction('sonata_seo_metadatas', [$this, 'getMetadatas'], ['is_safe' => ['html']]),
new TwigFunction('sonata_seo_html_attributes', [$this, 'getHtmlAttributes'], ['is_safe' => ['html']]),
new TwigFunction('sonata_seo_head_attributes', [$this, 'getHeadAttributes'], ['is_safe' => ['html']]),
Expand Down Expand Up @@ -83,6 +84,11 @@ public function getTitle()
return sprintf('<title>%s</title>', strip_tags($this->page->getTitle()));
}

public function getTitleText(): string
{
return $this->page->getOriginalTitle();
}

/**
* NEXT_MAJOR: remove this method.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Twig/Extension/SeoExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function testTitle()
static::assertSame('<title>foo bar</title>', $extension->getTitle());
}

public function getTitleText(): void
{
$page = $this->createMock(SeoPageInterface::class);
$page->expects(static::once())->method('getTitle')->willReturn('<b>foo bar</b>');

$extension = new SeoExtension($page, 'UTF-8');

static::assertSame('foo bar', $extension->getTitleText());
}

public function testEncoding()
{
$page = $this->createMock(SeoPageInterface::class);
Expand Down

0 comments on commit dff934e

Please sign in to comment.