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

Grav configuration in Javascript object

Started by Guillaume Storchi 6 years ago · 2 replies · 647 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 Vladimir Novak, 2 weeks ago
3 292 2 weeks ago
Plugins · by Mathieu Lecouturier, 3 weeks ago
3 277 3 weeks ago
Plugins · by Slebeig, 2 months ago
1 403 2 months ago
Plugins · by Rene, 2 months ago
2 574 2 months ago
Plugins · by Xavier, 3 months ago
2 490 3 months ago