Search code examples
twiggrav

Grav - Using shortcodes in twig


Following this question, I have found that there is a plugin named shortcodes which might be helpful here.

I have installed the plugin and created a new page:

---
title: slide1
media_order: slider1.jpeg
taxonomy:
    category:
        - 'Home Slider'
process:
    markdown: true
    twig: true
routable: false
cache_enable: false
visible: false
---

[section name="slide_title"]
Precisión y Calidad
[/section]

[section name="slide_subtitle"]
La máxima calidad y seguridad para nuestros clientes
[/section]

[section name="slide_link"]
www.google.es
[/section]

Then I have an invoked partial which makes:

{% for slide in taxonomy.findTaxonomy({'category': 'Home Slider'}) %}
    <span>TEST</span>
    <span>{{ slide.title }}</span>
    <span>{{ slide.shortcode.section.slide_title }}</span>
    <span>{{ slide.shortcode.section.slide_subtitle }}</span>
    <span>{{ slide.shortcode.section.slide_link }}</span>
{% endfor %}

But this generates just:

TEST
slide1 

And it does not output the shortcode section contents.


Solution

  • The documentation you linked to shows the correct syntax when you are not dealing directly with the page variable:

    {{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_title }}
    {{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_subtitle }}
    {{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_link }}