Jump to content

$config->scripts, how to add scripts at the top and the bottom of template?


Recommended Posts

Posted

If i have a template that requires scripts added in head  and at the end of body.

Do you have any idea how to handle this whith $config->scripts.

Another point is how to handle if its necessary to add not just files but small scripts that need to be added at the head or bottom.(like google analytics).

Is there something like :

$config->scripts-bottom

$config->scripts-direct

Maybe there is a way to make such an array, besides from silmply creating the var.

Posted

You could make your own:

// a filename array for scripts in the head (make the name you wish)
$config->headScripts = new FilenameArray();

// a filename array for scripts in the bottom (make the name you wish)
$config->bottomScripts = new FilenameArray();

// add scripts to the head FilenameArray
$config->headScripts->append('/your/wished/path/head.js');

// add scripts to the bottom FilenameArray
$config->bottomScripts->append('/your/wished/path/bottom.js');
  • Like 6
Posted

Is there something against abusing it this way ?

// a filename array abused  for plain scripts 
$config->headScripts = new FilenameArray();

// add scripts to the  FilenameArray
$config->headScripts->append("jQuery('a.gallery').colorbox(); ");

// this one s added in main.php
<? foreach($config->headScripts->unique() as $script) echo "\n\t<script type='text/javascript'>$script</script>";?>

  • 6 months later...
Posted

Just use RequireJS and in each page just load the scripts you need KISS because that method above doesn't handle dependency of libraries unlike RequireJS

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
×
×
  • Create New...