Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

JSON data from a plugin available to Twig - not working

Started by Oona O'Neil 9 years ago · 2 replies · 1243 views
9 years ago

I'm trying to make JSON data from a MailChimp (newsletter) plugin available to Twig.

I've added this code to my newsletter.php file:

$this->config->set('plugins.newsletter.X', $addMember);

and this to newsletter.md:

TWIG
{{ config.plugins.newsletter.X.addMember }}

as advised here.

When submitting the newsletter form I'm receiving the following error:

Use of undefined constant plugins - assumed 'plugins

Any ideas where I should define 'plugins' or how I should change the syntax?

Is it just me or 'X.whatever.variable' in:

{{ config.plugins.X.whatever.variable }}

is not very clear?

last edited 12/07/17 by Oona O'Neil
9 years ago

just because a json file exist in your pulgin doesn't mean you will be able to access it.. You need to load the file

PHP
$myjson = json_decode(file_get_contents('yourfile.json'))

Then you need to set that as a twig variable, probably in onTwigSiteVariables

PHP
$this->grav['twig']->twig_vars['myjson'] = $mnyjson;

Then in a Twig template you would be able to work with that as a PHP object.

👍 1
9 years ago

Thanks, I'll check that out. Is there any documentation about it you could point me to?

Suggested topics

Topic Participants Replies Views Activity
General · by themetman, 23 hours ago
2 40 16 hours ago
General · by Hanns Mattes, 1 week ago
0 121 1 week ago
General · by Karmalakas, 1 week ago
2 185 1 week ago
General · by Hanns Mattes, 3 weeks ago
0 235 3 weeks ago
General · by Dima, 4 weeks ago
1 211 4 weeks ago