Skip to content

Commit 2dd8856

Browse files
committed
update readme and things
1 parent a5b9eb0 commit 2dd8856

5 files changed

Lines changed: 54 additions & 41 deletions

File tree

README.md

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![](https://img.shields.io/packagist/v/heimrichhannot/contao-head-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-head-bundle)
44
[![](https://img.shields.io/packagist/dt/heimrichhannot/contao-head-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-head-bundle)
55

6-
This bundle enhances the handling of html `<head>` section tags. It provides services to update head tags dynamically from your code.
6+
Enhance your website's SEO and social media presence with more meta- and structured data.
77

88
## Features
99
- Provide a nice api to set head tags like meta, title, base, link
@@ -42,6 +42,46 @@ Following schema.org types are available:
4242
4343
![Screenshot Structured Data Settings](docs%2Fimg%2Fscreenshot_backend_structured_data.png)
4444
45+
### Add (meta-)tags from template (twig)
46+
47+
The bundle provides Twig functions to add head tags from Twig templates:
48+
49+
```twig
50+
{# Set tags #}
51+
{% do add_head_tag('title', 'Hello World') %}
52+
{% do add_head_tag('base', 'https://example.org') %}
53+
{# you can also set meta tags with meta_ prefix, but it is easier with add_head_meta_tag function #}
54+
{% do add_head_tag('meta_description', 'Lorem ipsum!') %}
55+
56+
{# Add meta tags #}
57+
{% do add_head_meta_tag('description', 'Lorem ipsum!') %}
58+
{% do add_head_meta_tag('og:title', 'Hello World') %}
59+
{% do add_head_meta_tag('twitter:image', figure) %}
60+
61+
{# Add multiple tags at once #}
62+
{# Pass as key => value or a AbstractHeadTag object #}
63+
{# Value can be a string, null or a Contao Figure. If a Figure is passed, its image source will be used as the tag content. #}
64+
{% do add_head_tags({
65+
'title': 'Hello World',
66+
'base': 'https://example.org',
67+
'meta_description': 'Lorem ipsum!',
68+
'meta_og:title': 'Hello World',
69+
'meta_twitter:image': figure,
70+
}) %}
71+
72+
{# Shorthand for news #}
73+
{%- do add_head_tags(get_head_news_tags(model, figure|default)) -%}
74+
```
75+
76+
| Function | Description |
77+
|-------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
78+
| `add_head_tag(name, value)` | Add all kinds of head tag. Meta tag names must be prefixed with `meta_`, for example `meta_description` or `meta_og:title`. |
79+
| `add_head_meta_tag(name, value)` | Shorthand for meta tags. The function adds the `meta_` prefix automatically. |
80+
| `add_head_tags(array)` | Add multiple tags at once. Pass as key => value or a `AbstractHeadTag` object. Meta tags must be prefixed with meta when passed as key. |
81+
| `get_head_news_tags(model, figure)` | Create an array of tags for news models. The function automatically adds og:title, og:description, og:url and og:image tags. Pass a figure to override default news article image. |
82+
83+
The `value` argument can be a string, `null` or a Contao `Figure`. If a `Figure` is passed, its image source is used as the tag content.
84+
4585
## Integration
4686
Use head bundle api set in your code.
4787

@@ -51,11 +91,12 @@ To set base, title, meta and link tags, use the `HtmlHeadTagManager` service:
5191

5292
```php
5393
use HeimrichHannot\HeadBundle\HeadTag\BaseTag;
94+
use HeimrichHannot\HeadBundle\HeadTag\LinkTag;
5495
use HeimrichHannot\HeadBundle\HeadTag\MetaTag;
55-
use HeimrichHannot\HeadBundle\HeadTag\TitleTag;
5696
use HeimrichHannot\HeadBundle\HeadTag\Meta\CharsetMetaTag;
5797
use HeimrichHannot\HeadBundle\HeadTag\Meta\HttpEquivMetaTag;
5898
use HeimrichHannot\HeadBundle\HeadTag\Meta\PropertyMetaTag;
99+
use HeimrichHannot\HeadBundle\HeadTag\TitleTag;
59100
use HeimrichHannot\HeadBundle\Manager\HtmlHeadTagManager;
60101
use Symfony\Component\HttpFoundation\Request;
61102

@@ -70,7 +111,7 @@ class SomeEventListener
70111

71112
//Set base tag from object or url
72113
$this->headTagManager->setBaseTag(new BaseTag($request->getSchemeAndHttpHost()));
73-
$this->headTagManager->setBaseTag('https://example.org'));
114+
$this->headTagManager->setBaseTag('https://example.org');
74115
}
75116

76117
public function updatedTitleTag(): void
@@ -79,7 +120,7 @@ class SomeEventListener
79120
$this->headTagManager->setTitleTag('Hello World');
80121

81122
// Set title tag from object and adjust output format
82-
$this->headTagManager->setTitleTag(new TitleTag('Foo Bar', '%s | {{page::rootPageTitle}}'))
123+
$this->headTagManager->setTitleTag(new TitleTag('Foo Bar', '%s | {{page::rootPageTitle}}'));
83124
// Will output: <title>Foo Bar | My Great Website Page Title</title>
84125
}
85126

@@ -122,36 +163,10 @@ class SomeEventListener
122163

123164
// Remove a tag
124165
$this->headTagManager->removeLinkTag('prev');
125-
126-
// Shorthand for canonical tag
127-
$this->headTagManager->setCanonical('https://example.org');
128166
}
129167
}
130168
```
131169

132-
### Set head content from Twig
133-
134-
The bundle provides Twig functions to add head tags from Twig templates:
135-
136-
```twig
137-
{# Set the title tag #}
138-
{% do add_head_tag('title', 'Hello World') %}
139-
140-
{# Set the base tag #}
141-
{% do add_head_tag('base', 'https://example.org') %}
142-
143-
{# Add meta tags #}
144-
{% do add_head_meta_tag('description', 'Lorem ipsum!') %}
145-
{% do add_head_meta_tag('og:title', 'Hello World') %}
146-
{% do add_head_meta_tag('twitter:image', figure) %}
147-
```
148-
149-
Use `add_head_tag(name, value)` for generic head tags supported by the `HeadTagFactory`. Meta tag names must be prefixed with `meta_`, for example `meta_description` or `meta_og:title`.
150-
151-
Use `add_head_meta_tag(name, value)` as shorthand for meta tags. The function adds the `meta_` prefix automatically.
152-
153-
The `value` argument can be a string, `null` or a Contao `Figure`. If a `Figure` is passed, its image source is used as the tag content.
154-
155170
## Template output
156171

157172
Be sure, `huh_head.use_contao_head` and/or `huh_head.use_contao_variables` are not set to true.

src/HeadTag/HeadTagFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
*/
2828
public function createTagByName(string $name, ?string $value = null): ?AbstractHeadTag
2929
{
30-
if ('base' === $name) {
30+
if ('base' === $name && is_string($value)) {
3131
return new BaseTag($value);
3232
}
3333

src/Manager/HtmlHeadTagManager.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use Contao\CoreBundle\InsertTag\InsertTagParser;
1212
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
1313
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
14-
use Contao\CoreBundle\String\HtmlAttributes;
15-
use Contao\StringUtil;
1614
use HeimrichHannot\HeadBundle\Exception\UnsupportedTagException;
1715
use HeimrichHannot\HeadBundle\HeadTag\AbstractHeadTag;
1816
use HeimrichHannot\HeadBundle\HeadTag\BaseTag;

src/Twig/HeadRuntime.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
use Symfony\Component\HttpFoundation\RequestStack;
99
use Twig\Extension\RuntimeExtensionInterface;
1010

11-
class HeadRuntime implements RuntimeExtensionInterface
11+
readonly class HeadRuntime implements RuntimeExtensionInterface
1212
{
1313
public function __construct(
14-
private readonly HtmlHeadTagManager $tagManager,
15-
private readonly RequestStack $requestStack,
14+
private HtmlHeadTagManager $tagManager,
15+
private RequestStack $requestStack,
1616
) {}
1717

1818
public function addHeadTag(string|AbstractHeadTag $name, string|Figure|null $value = null): void

src/Twig/IntegrationRuntime.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Contao\CoreBundle\Routing\ContentUrlGenerator;
99
use Contao\CoreBundle\String\HtmlDecoder;
1010
use Contao\NewsModel;
11-
use HeimrichHannot\HeadBundle\HeadTag\MetaTag;
11+
use HeimrichHannot\HeadBundle\HeadTag\Meta\PropertyMetaTag;
1212
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1313
use Symfony\Component\Uid\Uuid;
1414
use Twig\Extension\RuntimeExtensionInterface;
@@ -30,17 +30,17 @@ public function getNewsTags(NewsModel|int $model, mixed $figure = null): array
3030
}
3131
}
3232
$headTags = [
33-
new MetaTag('og:title', $this->htmlDecoder->inputEncodedToPlainText((string)$model->headline)),
34-
new MetaTag('og:description', $this->htmlDecoder->inputEncodedToPlainText((string)$model->teaser)),
35-
new MetaTag('og:url', $this->contentUrlGenerator->generate($model, referenceType: UrlGeneratorInterface::ABSOLUTE_URL)),
33+
new PropertyMetaTag('og:title', $this->htmlDecoder->inputEncodedToPlainText((string)$model->headline)),
34+
new PropertyMetaTag('og:description', $this->htmlDecoder->inputEncodedToPlainText((string)$model->teaser)),
35+
new PropertyMetaTag('og:url', $this->contentUrlGenerator->generate($model, referenceType: UrlGeneratorInterface::ABSOLUTE_URL)),
3636
];
3737

3838
if ($figure instanceof Figure) {
3939
$headTags['meta_og:image'] = $figure;
4040
} elseif ($model->addImage && $model->singleSRC) {
4141
try {
4242
$uuid = Uuid::fromBinary($model->singleSRC);
43-
$headTags[] = new MetaTag('og:image', (string)$this->filesStorage->generatePublicUri($uuid));
43+
$headTags[] = new PropertyMetaTag('og:image', (string)$this->filesStorage->generatePublicUri($uuid));
4444
} catch (\InvalidArgumentException | UnableToResolveUuidException) {
4545
}
4646
}

0 commit comments

Comments
 (0)