bytesource Posted January 25, 2013 Share Posted January 25, 2013 How to handle global variables using the API?I would like to output all javascript at the bottom of the page before the </body> tag. Right now I am somewhat ‘abusing’ the $config global variable for this, but I doubt that this is the right way to do:helpers.inc (included at the top of all templates) // variables used in main.inc $headline = ''; $content = ''; $sidenav = ''; $sidebar = ''; $config->js_config = ''; _projects.php $count = 1; echo "<ul id='filter-item'>"; foreach($page->galleries as $project) { // $page->galleries is a repeater field echo "<li data-id='id-1' class='grid_4' data-alpha='photography'>"; $gallery = new TemplateFile(wire('config')->paths->templates . 'snippets/images/single-link-gallery.php'); $gallery->set('title', $project->title); $gallery->set('id', $project->id); // id of the first image link of the gallery $gallery->set('gallery_id', $project->id); $gallery->set('images', $project->images); $gallery->set('thumb_width', 294); $gallery->set('thumb_height', 196); if($project->toggle_new) $gallery->set('new', true); echo $gallery->render(); echo "</li>"; $count++; } echo "</ul>"; single-link-gallery.php <?php $js = <<<JS imagebox.creategallery('$gallery_id', '$title', { galleryTitle: '%GALLERY%: %LIST%', continuousGalleries: 'true', }); JS; $config->js_config .= $js; ?> main.inc <script> <?php if ($config->js_config) echo $config->js_config; ?> </script> </body> Is there a better, more idiomatic way to achieve the above with the API?Cheers, Stefan Link to comment Share on other sites More sharing options...
Soma Posted January 25, 2013 Share Posted January 25, 2013 I don't see any abuse here. $config is a good way to do it, it's there for such things. 1 Link to comment Share on other sites More sharing options...
bytesource Posted January 25, 2013 Author Share Posted January 25, 2013 $config is a good way to do it, it's there for such things. That's good to know. Thanks a lot for your answer! Link to comment Share on other sites More sharing options...
ryan Posted January 27, 2013 Share Posted January 27, 2013 It should be fine to use $config so long as you aren't overwriting some property already in use. Though you might also look at using $page to store your runtime/temporary properties, as $page is more associated as a runtime variable. 1 Link to comment Share on other sites More sharing options...
bytesource Posted January 27, 2013 Author Share Posted January 27, 2013 Thanks for this tip! How could I have forgotten $page 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