Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Shortcodes break Twig processing in pages

Started by Muut Archive 10 years ago · 4 replies · 905 views
10 years ago

Using a stock install with Antimatter.

The page in question:

YAML
\```
title: Test
process:
    markdown: true
    twig: true
\```

This is a test page.

{{ test }}

Here is another line.

[section name="test"]
This line is in a shortcode.
[/section]

In antimatter.php:

PHP
<?php
namespace Grav\Theme;

use Grav\Common\Theme;

class Antimatter extends Theme
{
  public static function getSubscribedEvents()
  {
    return [
      'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
    ];
  }

  public function onTwigSiteVariables()
  {
    $this->grav['twig']->twig_vars['test'] = 'Hello World!';
  }
}

When I add the shortcode, neither the shortcode nor the Twig variable gets processed.

It seems like either Twig or shortcodes can be processed, but not both.

Am I missing something?

10 years ago

Here's the template that the page is using:

default.html.twig

TWIG
{% extends 'partials/base.html.twig' %}

{% block content %}
    {{ page.content }}

    <h2>Extra Content</h2>

    <p>
        {{ shortcode.section.test }}
    </p>
{% endblock %}

10 years ago

They should not interfere. Will test real quick. stand by....

10 years ago

Ok easy fix! I just realized you have your Twig var being set by onTwigSiteVariables() which runs after the page is processed. What you need is onTwigPageVariables() to make it available for the page.

The way you had it, you could of put the {{ test }} in the template's TWIG file, and it would of showed fine, but to be available in both pages and the site templates, you need to set that that even to onTwigPageVariables().. Here's my test:

PHP

    public static function getSubscribedEvents()
    {
        return [
            'onTwigPageVariables' => ['onTwigPageVariables', 0],
            'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
        ];
    }

    public function onTwigPageVariables()
    {
        $this->grav['twig']->twig_vars['test'] = 'Hello World! in Page';
    }

    public function onTwigSiteVariables()
    {
        $this->grav['twig']->twig_vars['test'] = 'Hello World! in Site';
    } 
---
10 years ago

Fantastic!
The Grav team is astronomically awesome!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1360 9 years ago
Archive · by Muut Archive, 9 years ago
2 939 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2958 9 years ago
Archive · by Muut Archive, 9 years ago
3 1122 9 years ago