InlineJsModule.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * @package Grav\Common\Assets
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 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. /**
  11. * Class InlineJs
  12. * @package Grav\Common\Assets
  13. */
  14. class InlineJsModule extends BaseAsset
  15. {
  16. /**
  17. * InlineJs constructor.
  18. * @param array $elements
  19. * @param string|null $key
  20. */
  21. public function __construct(array $elements = [], ?string $key = null)
  22. {
  23. $base_options = [
  24. 'asset_type' => 'js_module',
  25. 'attributes' => ['type' => 'module'],
  26. 'position' => 'after'
  27. ];
  28. $merged_attributes = Utils::arrayMergeRecursiveUnique($base_options, $elements);
  29. parent::__construct($merged_attributes, $key);
  30. }
  31. /**
  32. * @return string
  33. */
  34. public function render()
  35. {
  36. return '<script' . $this->renderAttributes(). ">\n" . trim($this->asset) . "\n</script>\n";
  37. }
  38. }