vaersaagod/seomate

Allow using schema using {% do %} tag

Closed this issue · 6 comments

Right now we can use schema like this. This code cannot be utilized outside {% block %} statements:

    {% set schema = craft.schema %}
    {{ schema.article
        .headline(entry.title)
    )
    | raw }}

Would be great if we could just use it like this:

    {% do craft.schema.article
        .headline(entry.title)
    ) %}

This way we would be able to use it outside block statements.

Already possible, you just have to set the schema to a variable first, then operate on that variable:

{% set schema = craft.schema.article %}
{% do schema.headline(entry.title) %}

@mmikkel
Sorry, but this does not produce any results. I am using 2.1.2 plugin version.

@piotrpog Sorry, I might've misunderstood what you were asking in the first place. I assume you're actually looking to register/enqueue JSON-LD schemas from within Twig partials?

SEOMate isn't able to do that, and this isn't a feature we're likely to add.

I can think of a couple of ways to build a system like it yourself, though – for example you can probably use the new "global scope" feature in Craft 4.5 to "collect" schemas in Twig partials, rendering them in your layout template, i.e. something like

{% set schema = craft.schema.article.headline(entry.title) %}
{% do _globals.set('schemas', _globals.get('schemas')|default([])|push(schema) %}

...

{% set schemas = _globals.get('schemas')|default([]) %}
{{ schemas|join|raw }}

Another option is to create a {% block schema %} block in your layout template, and do something like this in templates extending that layout:

{% block schema %}
    {{ parent() }}
    {{ craft.schema.article
        .headline(entry.title)
    )
    | raw }}
{% endblock %}

@mmikkel
Thanks for the explanation, but i wasnt trying to register schemas from multiple template components. Just from one entry template, to add schema specific for the article. And i was trying to use {% do %} statement to avoiding putting seomate code withing content blocks.

@piotrpog In that case, creating a dedicated {% block schema %}{% endblock %} block in your layout template sounds like a good solve?

@mmikkel yeah, sounds like a good idea, thanks.