js($path, $async = false)

Creates a script tag to load a javascript file

  • $path (string)
    Relative or absolute URL
  • $async (boolean|array)
    Either true for the async attribute or an array of attributes
  • return (string)

Example

Creating a single script tag

<?= js('assets/js/site.js') ?>

Creating multiple script tags

<?= js(array(
  'assets/js/jquery.js',
  'assets/js/jquery.ui.js',
  'assets/js/site.js',
)) ?>

Autoloading template specific script files

<?= js('@auto') ?>

Template specific js files must be located in /assets/js/templates and named like the template.

Template JS file
/site/templates/project.php /assets/js/templates/project.js
/site/templates/home.php /assets/js/templates/home.js
/site/templates/blog.php /assets/js/templates/blog.js

Async

If you want the js files to be loaded asynchronously (if supported by the browser), you can set a second parameter as true:

<?= js('assets/js/site.js', true) ?>

2.2.3 +

This also works with an array of paths:

<?= js(array(
  'assets/js/jquery.js',
  'assets/js/jquery.ui.js',
  'assets/js/site.js',
), true) ?>

2.5.2 +

Other attributes

You can also pass an array of completely custom attributes like the defer attribute:

<?= js('assets/js/site.js', ['async' => true, 'defer' => true, 'data-something' => 'my-value']) ?>