JSON data from a plugin available to Twig - not working

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:

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

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

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

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

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

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