JoppeDC / SyliusBetterSeoPlugin

SEO Plugin for Sylius products and taxons
MIT License
22 stars 21 forks source link

Figure out a way to render block in head #1

Open JoppeDC opened 4 years ago

JoppeDC commented 4 years ago

Currently we're using javascript to create meta tags in the header, from the body. The cleanest way would be if we could render the _seo_block directly in the header itself.

This can be done using the sonata block render event sylius.shop.layout.head. The problem is that we can't access the product in this context.

Prometee commented 4 years ago

Maybe using another event related to the product show page would be a better idea and then adding a block extending the metatags block into the twig file linked to this event.

Here is the template : https://github.com/Sylius/Sylius/blob/master/src/Sylius/Bundle/ShopBundle/Resources/views/Product/show.html.twig Then choose a generic event (eg: one who will not be removed by a custom logic), maybe the sylius.shop.product.show.after_product_header event is the best choice. Into the twig file affected to this event add this kind of code :

{% block metatags %}
    {{ parent() }}
    {# add those custom meta tags #}
{% endblock %}
JoppeDC commented 4 years ago

@Prometee That is an excellent idea. There is already a metatags block in the default layout template, if we can extend this block it can be rendered in the head directly without the Javascript functions! I'm going to give it a try 😏

JoppeDC commented 4 years ago

@Prometee Only issue i'm guessing is that you cant just extend blocks in a sonata block event. I've done a quick test on a clean project and it wont let me render blocks in an event template. If i include the layout.html.twig it'll just render the page inside itself

JoppeDC commented 4 years ago

@prometee Feel free to give it a try! If it works, just open a PR and i’ll check it out 😉

Prometee commented 4 years ago

@JoppeDC So I think the only way is to make a twig extension, it will allow use to make everything we want... I'm trying to understand how it could be done right I will made a PR if I found a way.

tuala commented 4 years ago

I've been looking at the Setono's Tag Bag Plugin / Sylius Analytics Plugin, they put their library tags in the head thru event suscribers probably some good inspiration there.

JoppeDC commented 4 years ago

@tuala thanks for the heads up! I’ll definetly give it a look 😉