Back to HTMLHelper class

Method stylesheet

public static array|string|null
stylesheet
(mixed $file, mixed $options = array(), mixed $attribs = array())
Write a `<link>` element to load a CSS file
Parameters
  • string $file Path to file
  • array $options Array of options. Example: array('version' => 'auto', 'conditional' => 'lt IE 9')
  • array $attribs Array of attributes. Example: array('id' => 'scriptid', 'async' => 'async', 'data-test' => 1)
Returns
  • array|string|null nothing if $returnPath is false, null, path or array of path if specific CSS browser files were detected
Since
  • 1.5
-
  • \Joomla\CMS\Environment\Browser
Class: HTMLHelper
Project: Joomla

Method stylesheet - Source code

/**
 * Write a `<link>` element to load a CSS file
 *
 * @param   string  $file     Path to file
 * @param   array   $options  Array of options. Example: array('version' => 'auto', 'conditional' => 'lt IE 9')
 * @param   array   $attribs  Array of attributes. Example: array('id' => 'scriptid', 'async' => 'async', 'data-test' => 1)
 *
 * @return  array|string|null  nothing if $returnPath is false, null, path or array of path if specific CSS browser files were detected
 *
 * @see   Browser
 * @since 1.5
 */
public static function stylesheet($file, $options = array(), $attribs = array())
{
    $options['relative'] = $options['relative'] ?? false;
    $options['pathOnly'] = $options['pathOnly'] ?? false;
    $options['detectBrowser'] = $options['detectBrowser'] ?? false;
    $options['detectDebug'] = $options['detectDebug'] ?? true;
    $includes = static::includeRelativeFiles('css', $file, $options['relative'], $options['detectBrowser'], $options['detectDebug']);
    // If only path is required
    if ($options['pathOnly']) {
        if (\count($includes) === 0) {
            return;
        }
        if (\count($includes) === 1) {
            return $includes[0];
        }
        return $includes;
    }
    // If inclusion is required
    $document = Factory::getApplication()->getDocument();
    foreach ($includes as $include) {
        // If there is already a version hash in the script reference (by using deprecated MD5SUM).
        if ($pos = strpos($include, '?') !== false) {
            $options['version'] = substr($include, $pos + 1);
        }
        $document->addStyleSheet($include, $options, $attribs);
    }
}