Skip to content

Instantly share code, notes, and snippets.

@shoman4eg
Created March 22, 2017 11:36
Show Gist options
  • Save shoman4eg/e6e1fd0d5a226a418fd45dccc9a8f98d to your computer and use it in GitHub Desktop.
Save shoman4eg/e6e1fd0d5a226a418fd45dccc9a8f98d to your computer and use it in GitHub Desktop.
Bitrix js in footer
<?php
use Bitrix\Main\Page\Asset;
class FooterAsset
{
const LOCATION = 'FOOTER_LOCATION';
public static function addJs($src, $options = [])
{
$options['src'] = $src;
$attributes = implode(' ', array_map(function ($key, $value) {
if($value === true) {
return $key;
} else {
return sprintf('%s="%s"', $key, $value);
}
}, array_keys($options), $options));
Asset::getInstance()->addString("<script {$attributes}></script>", false, FooterAsset::LOCATION);
}
public static function show()
{
global $APPLICATION;
$APPLICATION->AddBufferContent([Asset::getInstance(), 'getStrings'], FooterAsset::LOCATION);
}
}
// use
FooterAsset::addJs('/path/to/file.js', ['async' => true, 'type'=>"text/javascript", 'data-type' => 'footer-script']);
?>
<html>
<head>
</head>
<body>
<footer>
</footer>
<? FooterAsset::show() ?>
</body>
</html>
В итоге получится:
<html>
<head>
</head>
<body>
<footer>
</footer>
<script async type="text/javascript" data-type="footer-script" src="/path/to/file.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment