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.

Plugins

Grav configuration in Javascript object

Started by Guillaume Storchi 6 years ago · 2 replies · 540 views
6 years ago

Is there any danger to expose grav config in a javascript object for frontend parametrisation ?

I do this in a plugin
public functionn onPluginsInitialized() {
//map plugin config
$configJSON = json_encode($this->grav['config']);
$assets->addInlineJs("var GRAV = {};GRAV.config = JSON.parse('" . addslashes($configJSON) . "');", ['loading'=>'inline', 'position'=>'before']);.
}

I need to get page frontmatter configuration as well, in order to work with more advanced javascript.

If there is any other proper way to achieve this, I woul dbe happy to know it.

More generally is there any form of way of working with a assets vue.js or any mordern js framework in themes or plugins ?

That would be a great point to deal with in documentation, in order to get more developpers in grav cms !!

Thanks in advance

6 years ago

Exposing internal data in the frontend is asking for trouble. You might think it all is pretty harmless but you'll be amazed what hackers can do with such information. And if you are amazed it's too late.

So I would be very selective and only expose the data you absolutely need.
The way you transfer the config data to Javascript is fine I think.

In future please use the triple quotes block in this editor. Your code (with one extra line) then looks like this:

PHP
public functionn onPluginsInitialized() {
    //map plugin config
    $configJSON = json_encode($this->grav['config']);
    $assets = Grav::instance()['assets'];
    $assets->addInlineJs("var GRAV = {};GRAV.config = JSON.parse('" . addslashes($configJSON) . "');", ['loading'=>'inline', 'position'=>'before']);
}

Even more important is that single and double quotes are the correct characters instead of their curly look alike (for example " vs "). It makes a quick copy/paste much easier and thus helps in getting answers.

6 years ago

Ok thanks for your reply. I think It is a important topic, that should be exposed in documentation.
Thanks!

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 47 1 week ago
Plugins · by Xavier, 4 weeks ago
2 56 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1182 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 50 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 74 2 months ago