Back to ScriptsRenderer class

Method renderElement

private string
renderElement
(mixed $item)
Renders the element
Parameters
  • \Joomla\CMS\WebAsset\WebAssetItemInterface|array $item The element
Returns
  • string The resulting string
Since
  • 4.0.0

Method renderElement - Source code

/**
 * Renders the element
 *
 * @param   WebAssetItemInterface|array  $item  The element
 *
 * @return  string  The resulting string
 *
 * @since   4.0.0
 */
private function renderElement($item) : string
{
    $buffer = '';
    $asset = $item instanceof WebAssetItemInterface ? $item : null;
    $src = $asset ? $asset->getUri() : $item['src'] ?? '';
    // Make sure we have a src, and it not already rendered
    if (!$src || !empty($this->renderedSrc[$src]) || $asset && $asset->getOption('webcomponent')) {
        return '';
    }
    $lnEnd = $this->_doc->_getLineEnd();
    $tab = $this->_doc->_getTab();
    $mediaVersion = $this->_doc->getMediaVersion();
    // Get the attributes and other options
    if ($asset) {
        $attribs = $asset->getAttributes();
        $version = $asset->getVersion();
        $conditional = $asset->getOption('conditional');
        // Add an asset info for debugging
        if (JDEBUG) {
            $attribs['data-asset-name'] = $asset->getName();
            if ($asset->getDependencies()) {
                $attribs['data-asset-dependencies'] = implode(',', $asset->getDependencies());
            }
        }
    } else {
        $attribs = $item;
        $version = isset($attribs['options']['version']) ? $attribs['options']['version'] : '';
        $conditional = !empty($attribs['options']['conditional']) ? $attribs['options']['conditional'] : null;
    }
    // To prevent double rendering
    $this->renderedSrc[$src] = true;
    // Check if script uses media version.
    if ($version && strpos($src, '?') === false && ($mediaVersion || $version !== 'auto')) {
        $src .= '?' . ($version === 'auto' ? $mediaVersion : $version);
    }
    $buffer .= $tab;
    // This is for IE conditional statements support.
    if (!\is_null($conditional)) {
        $buffer .= '<!--[if ' . $conditional . ']>';
    }
    // Render the element with attributes
    $buffer .= '<script src="' . htmlspecialchars($src) . '"';
    $buffer .= $this->renderAttributes($attribs);
    $buffer .= '></script>';
    // This is for IE conditional statements support.
    if (!\is_null($conditional)) {
        $buffer .= '<![endif]-->';
    }
    $buffer .= $lnEnd;
    return $buffer;
}