Jump to content

Is there an idiomatic way to handle global variables using the API?


bytesource
 Share

Recommended Posts

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

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. 

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...