Js.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /**
  3. * @package Grav\Common\Assets
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Assets;
  9. use Grav\Common\Utils;
  10. class Js extends BaseAsset
  11. {
  12. public function __construct(array $elements = [], $key = null)
  13. {
  14. $base_options = [
  15. 'asset_type' => 'js',
  16. ];
  17. $merged_attributes = Utils::arrayMergeRecursiveUnique($base_options, $elements);
  18. parent::__construct($merged_attributes, $key);
  19. }
  20. public function render()
  21. {
  22. if (isset($this->attributes['loading']) && $this->attributes['loading'] === 'inline') {
  23. $buffer = $this->gatherLinks( [$this], self::JS_ASSET);
  24. return '<script' . $this->renderAttributes() . ">\n" . trim($buffer) . "\n</script>\n";
  25. }
  26. return '<script src="' . trim($this->asset) . $this->renderQueryString() . '"' . $this->renderAttributes() . "></script>\n";
  27. }
  28. }