Pinion: perch_get_javascript()
The perch_get_javascript()
function insert links to your Javascript files into your documents.
Default
perch_get_javascript();
By default it inserts links to all the Javascript files that are placed in the folder you have defined with PIPIT_PINION_ASSETS_DIR
in your perch/addons/feathers/pipit_pinion/config.php
. This includes files that are placed in sub-folders.
If you have PERCH_PRODUCTION_MODE
set to PERCH_DEVELOPMENT
in the Perch config file, the function inserts links to all the Javascript files that are placed in the folder you have defined with PIPIT_PINION_ASSETS_DEV_DIR
instead.
Parameters
Type | Description |
---|---|
Array | Options array, see table below |
Options Array
Name | Value |
---|---|
dir | The name of a directory from which to link JS files. Not to be used with the file option. Path is relative to root. |
files | An array. Paths of one or more JS files to link. Paths are relative to root. |
pre | An array. Used to specify which files are added first. |
exclude | An array that specifies files to be excluded. |
attrs | An array. Used to add attributes to the generated tags. |
cache-bust | Boolean or Array. Set to true to apply to all files, or specify files in an array |
dev | Set to true to only link files in PERCH_DEVELOPMENT |
Usage examples
Adding all JS files in a directory (and sub-directories)
If you have a folder that has a mix of file types in different sub-folders, but you only want to add the (keep explaining).
perch_get_javascript([
'dir' => 'src/vendor',
]);
Order control
You can control which files are added first to your document with the pre
option. Note that files in sub-folders need to include the folder name.
perch_get_javascript([
'dir' => 'src/vendor',
'pre' => ['jquery/jquery.min.js'],
]);
Adding specific files with attributes
This adds the specified files and add attributes to the generated tag for zenscroll-min.js
:
perch_get_javascript([
'files' => ['src/vendor/zenscroll-min.js', 'jquery/jquery.min.js'],
'attrs' => [
'src/vendor/zenscroll-min.js' => ['async'=>true, 'charset'=>'UTF-8'],
],
]);
Adding attributes to multiple files
If you know zenscroll.js
and prism/prism.js
will be included, you can add different attributes to each one.
perch_get_javascript([
'dir' => 'src/vendor',
'attrs' => [
'prism/prism.js' => ['async'=>true, 'charset'=>'UTF-8'],
'zenscroll-min.js' => ['defer'=>true]
],
]);
Adding a single file for development only
Only add accessibility.js
when PERCH_PRODUCTION_MODE
set to PERCH_DEVELOPMENT
.
perch_get_javascript([
'files' => ['src/accessibility.js'],
'dev' => true,
]);