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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Shortcode access to page.header in twig

Started by Richard Hainsworth 8 years ago · 4 replies · 1214 views
8 years ago

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="{{ myvar.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:

PHP
$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

YAML
---
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?

8 years ago

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

8 years ago

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

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

8 years ago

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

8 years ago

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:

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

... means other options needed for shortcode.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 101 17 hours ago
General · by pamtbaau, 22 hours ago
1 68 22 hours ago
General · by Andy Miller, 1 day ago
0 53 1 day ago
General · by Marcel, 12 months ago
6 361 5 days ago
General · by Duc , 6 days ago
3 49 6 days ago