Hi, I couldnt find any solution for this.
I am loading multiple javascript files in my template:

scripts[] = js/swiper-bundle.min.js
scripts[] = js/custom-apps.js
scripts[] = js/custom-leaflet.js
...
Some of the files I want to render in the header and some in the footer.

But this doesnt work:

scripts["header"] = js/swiper-bundle.min.js
scripts["footer"] = js/custom-apps.js
scripts["footer"] = js/custom-leaflet.js

<?php print backdrop_get_js('header'); ?>
<?php print backdrop_get_js('footer'); ?>

 

Accepted answer

Hello @webtransformer 👋

Have you tried https://api.backdropcms.org/api/backdrop/core%21includes%21common.inc/fu... ?

I believe that something like this would work:

backdrop_add_js(path_to_theme() . 'js/swiper-bundle.min.js', array(
    'type' => 'file',
    'scope' => 'header',
  )
);
backdrop_add_js(path_to_theme() . 'js/custom-apps.js', array(
    'type' => 'file',
    'scope' => 'footer',
    'weight' => 1,
  )
);
backdrop_add_js(path_to_theme() . 'js/custom-leaflet.js', array(
    'type' => 'file',
    'scope' => 'footer',
    'weight' => 2,
  )
);

Please give that a go and let us know how it goes.

Comments

Does this work in drupal thought? I cant find doc that show this.

klonos's picture

Hello @webtransformer 👋

Have you tried https://api.backdropcms.org/api/backdrop/core%21includes%21common.inc/fu... ?

I believe that something like this would work:

backdrop_add_js(path_to_theme() . 'js/swiper-bundle.min.js', array(
    'type' => 'file',
    'scope' => 'header',
  )
);
backdrop_add_js(path_to_theme() . 'js/custom-apps.js', array(
    'type' => 'file',
    'scope' => 'footer',
    'weight' => 1,
  )
);
backdrop_add_js(path_to_theme() . 'js/custom-leaflet.js', array(
    'type' => 'file',
    'scope' => 'footer',
    'weight' => 2,
  )
);

Please give that a go and let us know how it goes.

Thanks it worked. I added this to template.php

function XXX_preprocess_page(&$variables) {

  backdrop_add_js(drupal_get_path('theme', 'XXX') . '/js/cookieconsent.min.js', array(
    'type' => 'file',
    'scope' => 'footer',
  )
);
}

And this to my page template:
<?php print backdrop_get_js("footer"); ?>

Works also with aggregation  :-)

Thank you very much!