Passing dashboard config variable to a globally loaded javascript file
Permalink
I'm building an addon that adds a javascript footer item to all non-admin pages. I have a dashboard page where settings can be applied, but how can I load the variables stored in the package config from my external javascript file?
Typically when building a block, I would output the variables as data attributes on one of the dom elements in the blocks view, then my external javascript could read those attributes and act on them. An example would be a javascript maps plugin that loads the address data from a dom element.
But I'm stuck on this one, because I can't figure out how to load the data into my javascript file when there is no dom element I can print the variables to.
I tried to add a footer item as script.php instead of script.js and echo the javascript from there, but doing so results in an "Access Denied" because its outside the scope of the C5 controller.
Is there a way I could save the config variables to a JSON file... or is there a way for my javascript file to make an ajax call to a php file in my package without it being Access Denied via c5 execute or die?
Typically when building a block, I would output the variables as data attributes on one of the dom elements in the blocks view, then my external javascript could read those attributes and act on them. An example would be a javascript maps plugin that loads the address data from a dom element.
But I'm stuck on this one, because I can't figure out how to load the data into my javascript file when there is no dom element I can print the variables to.
I tried to add a footer item as script.php instead of script.js and echo the javascript from there, but doing so results in an "Access Denied" because its outside the scope of the C5 controller.
Is there a way I could save the config variables to a JSON file... or is there a way for my javascript file to make an ajax call to a php file in my package without it being Access Denied via c5 execute or die?
Ok so I figured in my packages on_start, I can actually echo a div to the dom with the variables stored there. Not sure if this is the best way to do it, but it does work...
// Large json file
{"pages": [ {"page": "..."}, { ... } ... ]}
{"pages": [ {"page": "..."}, { ... } ... ]}
I don't understand what your trying to say.
I figured out the correct way to do this. Echoing a div out to the dom is not the correct way.
The correct process of passing php variables to javascript with no dom element to append attributes to (such as when you have a block view) is to create a custom route that routes to a class function that outputs the json. Then the javascript can request the route as if it was a file itself on your website and get the returned json. Thanks to JohntheFish for helping me with that one.
The correct process of passing php variables to javascript with no dom element to append attributes to (such as when you have a block view) is to create a custom route that routes to a class function that outputs the json. Then the javascript can request the route as if it was a file itself on your website and get the returned json. Thanks to JohntheFish for helping me with that one.
Could anyone please translate the 2nd paragraph to English?
I need to set a variable in the Dashboard (a page attribute) and based on its value load JS for either some pages having the set attribute or for all pages on site.
I need to set a variable in the Dashboard (a page attribute) and based on its value load JS for either some pages having the set attribute or for all pages on site.
"Typically when building a block, I would output the variables as data attributes on one of the dom elements in the blocks view, then my external javascript could read those attributes and act on them. An example would be a javascript maps plugin that loads the address data from a dom element."
Translation:
When passing information from a block controller to its view, I can simply echo the information in php into the block's html markup via html attributes such as data-custom="something". But how should I pass information stored in the dashboard to the view, since there is no block output to echo this information into ( information which is then acted upon via the javascript). The answer to the question is using a custom route in the package controller.
I needed to learn this so I could pass information stored in the conrete5 database to javascript code loaded on the front end without having any html elements to print the database info to as I could do if it was a block controller I was coding from. My plugins Styled Scroll Bar and Smooth Tag are the ones I made while learning this technique.
Translation:
When passing information from a block controller to its view, I can simply echo the information in php into the block's html markup via html attributes such as data-custom="something". But how should I pass information stored in the dashboard to the view, since there is no block output to echo this information into ( information which is then acted upon via the javascript). The answer to the question is using a custom route in the package controller.
I needed to learn this so I could pass information stored in the conrete5 database to javascript code loaded on the front end without having any html elements to print the database info to as I could do if it was a block controller I was coding from. My plugins Styled Scroll Bar and Smooth Tag are the ones I made while learning this technique.