vaersaagod / seomate

SEO, mate! It's important. That's why SEOMate provides the tools you need to craft all the meta tags, sitemaps and JSON-LD microdata you need - in one highly configurable, open and friendly package - with a super-light footprint.
MIT License
36 stars 8 forks source link

Allow using schema using {% do %} tag #76

Closed piotrpog closed 1 year ago

piotrpog commented 1 year ago

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.

mmikkel commented 1 year ago

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) %}
piotrpog commented 1 year ago

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

mmikkel commented 1 year ago

@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 %}
piotrpog commented 1 year ago

@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.

mmikkel commented 1 year ago

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

piotrpog commented 1 year ago

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