bernhard Posted August 25, 2016 Share Posted August 25, 2016 i need to share some variables between my backend and frontend. i tried using $config->js for that: https://processwire.com/api/ref/config/js/ the strange thing is that it throws an "Uncaught ReferenceError: ProcessWire is not defined" error - but only when i am not logged in! i tried it on a different install (both pw3) and there is the same problem. is that intended? what can be the problem? _main.php [...] $config->js('test', 'bla'); ?> <script> $(document).ready(function() { console.log(ProcessWire.config.test); }); </script> ok without the document.ready i get the error both logged in and logged out... even more strange any ideas? Link to comment Share on other sites More sharing options...
tpr Posted August 25, 2016 Share Posted August 25, 2016 Js would try to add properties to ProcessWire obj but on front end it's not available. Try var ProcessWire = ProcessWire || {}; but perhaps you should add Js another way instead. At least that's my theory Plus ProcessWire.config will be unavailable too. 1 Link to comment Share on other sites More sharing options...
horst Posted August 25, 2016 Share Posted August 25, 2016 I think it is for security reasons too, that not everyone can read config settings. Link to comment Share on other sites More sharing options...
Soma Posted August 25, 2016 Share Posted August 25, 2016 Why should it not be available on the front-end? It's just you have to output the stuff in your header. Look at the default.php of templates-admin/. I always use it in my sites. <script type="text/javascript"> var config = <?php echo json_encode($jsConfig); ?>; </script> Then use the config.key in your JS. 7 Link to comment Share on other sites More sharing options...
szabesz Posted August 25, 2016 Share Posted August 25, 2016 17 hours ago, Soma said: I always use it in my sites. I can prove that he does so: He even writes tutorials about the topic: http://soma.urlich.ch/posts/custom-js-in-processwire-admin/ 5 Link to comment Share on other sites More sharing options...
bernhard Posted August 26, 2016 Author Share Posted August 26, 2016 thank you guys, especially @Soma, helped me a lot! i totally forgot i had to inject the variable on my own in frontend. when i was logged in it was present because i use frontendediting and there it gets injected automatically. totally makes sense i took a slightly different approach with my own js variable: // template <?php echo $modules->get('PwFullCalendar')->render(array( 'editable' => $page->editable(), )); ?> // module public function render($options = array()) { // get settings $settings = array_merge($this->settings, $options); extract($settings); // push settings to javascript $out = '<script>var fullcalsettings = ' . json_encode($settings) . ';</script>'; 3 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