protro Posted November 4, 2022 Share Posted November 4, 2022 I am trying to retrieve a config value to be shared with javascript using a $config->js() declaration at the top of _main.php: $config->js('mySettings', [ 'lightLogo' => $pages->get('logo-alpha-light')->image->url, 'darkLogo' => $pages->get('logo')->image->url, ]); in main.js when I try to access mySettings the console shows: ReferenceError: ProcessWire is not defined What am I missing? This is the first line in main.js var mySettings = ProcessWire.config.mySettings; Link to comment Share on other sites More sharing options...
flydev Posted November 4, 2022 Share Posted November 4, 2022 @protro do you get same result with jsConfig() ? Also, I think it's only exposed on admin pages by default. For frontend you will need (as always) to make it available yourself, as nothing is done on frontend things by default, except processwire api: On your <head> tag in frontend, eg. _main.php : <!-- credit to @soma --> <script type="text/javascript"> var config = <?php echo json_encode($jsConfig); ?>; </script> Then // Set a property from PHP // $config->js() is still here for backward compatibility, use jsConfig() $config->jsConfig('mySettings', [ 'foo' => 'bar', 'bar' => 123, ]); // Get a property (from PHP) $mySettings = $config->jsConfig('mySettings'); // Get a property (from Javascript): var mySettings = ProcessWire.config.mySettings; console.log(mySettings.foo); console.log(mySettings.bar); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now