Shortcode access to page.header in twig


I’m extending my map-marker-leaflet plugin.

When style is set explicitly, eg., [map-marker-leaflet style=outdoors] ... the plugin correctly assigns the style to the map (for a map provider with that style).

When set using a twig variable, eg. [map-marker-leaflet style="{{ }}"] ... (and so long as a twig variable myvar has been created for that page), then the plugin correctly assigns the style.

This is done within the shortcode php file as follows:

$params = $sc->getParams(); //$sc is passed by the shortcode function
if (isset($params['style'])) {
    $style = $this->grav['twig']->processString($params['style']);
} else $style = '';

But now I want to have [map-leaflet style="{{ page.header.mapstyle }}"... with the page header being

mapstyle: outdoors

However, within the shortcode plugin, $style is ending up as null.

It would seem that within $this->grav['twig']->processString() there is no access to the page.header object.

The grav['page'] object is available inside the shortcode, but this means analyzing the twig string for page.header and getting the field.

processString() will also take an array of variables, but I do not see how to change the object grav['page']->header() into a suitable array.

What am I missing?

Is there a shortcode plugin that accesses the page header in a similar way?


Is the page being called directly or as part of a collection? If the individual page is being called as a collection, the shortcode can’t see that specific page. grav['page'] points to the containing page. (See issue 1967; no current workaround.)


So far I am developing only as a single page called directly.

As I said, within the shortcode’s php file (extending the Shortcode Class), I can access grav['page'], so in fact I can explicitly access eg. page.header.mapstyle.

But what I am looking for is a way of processing a generic twig string, as one would in a normal md file.

The problem is that a shortcode is processed before the Twig. So for instance, if I have in the md file something like

mapstyle: outdoors
[map-leaflet style="{{page.header.mapstyle}}" ] [/map-leaflet]

Then in the shortcode the value of $params['mapstyle'] (created in the generic manner) is in fact {{page.header.mapstyle}} and not outdoors.

Consequently, I am trying to find a mechanism for processing a generic string, for example page.header.mapstyle to get outdoors within the shortcode php.

If the string contains twig variables, then I get my solution by calling grav['twig']->processString($params['style']), but this generates null or '' if the string references a page.header.


Have you tried setting twig_first: true to true? Does that help?


Sorry for long delay - other tasks.

No, twig_first: true does not work. When I dump the value of $params['style'], I get "page.header.map_style".

The relevant page is:

   twig_first: true
map_style: neighbourhood
[map-marker-leaflet style="{{ page.header.map_style }}" ... ][/map-marker-leaflet]

... means other options needed for shortcode.