Setting Seo Metadata from Twig

1.3 version
Maintained

Setting Seo Metadata from Twig

This bundle provides a twig function cmf_seo_update_metadata which lets you populate the SEO data from an object. For details on how populating the SEO data works, read SeoBundle.

If you use this function, you must make sure to call this in your twig template before the calls to sonata_seo_title and sonata_seo_metadatas The recommended set up for this is to create a metadata block in your base twig template and override it in a sub template, calling cmf_seo_update_metadata before calling parent().

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- app/Resources/views/base.html.twig -->
<!DOCTYPE html>
<html>
    <head>
        {% block metadata %}
            {{ sonata_seo_title() }}

            {{ sonata_seo_metadatas() }}
        {% endblock metadata %}
    </head>
    <body>
        <p>Some page body.</p>
    </body>
</html>

<!-- app/Resources/views/blog/post.html.twig -->
{% extends 'base.html.twig' %}

{% block metadata %}
    {% do cmf_seo_update_metadata(post) %}

    {{ parent() }}
{% endblock metadata %}

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.