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

JSON data from a plugin available to Twig - not working

Started by Oona O'Neil 9 years ago · 2 replies · 787 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 Jerry Hunt, 4 days ago
2 76 7 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago