N
N
nepster-web2014-03-06 15:23:05
Yii
nepster-web, 2014-03-06 15:23:05

How to add additional attribute in css files connection in Yii2?

The task is this, there are several css-files, in one of the css-files there is a parameter: media="print" , without it the layout goes.
This is how I include my css files:

class AppAsset extends AssetBundle
{
  public $sourcePath = '@frontend/modules/site/assets';
  public $css = [
        'http://fonts.googleapis.com/css?family=Patua+One|Montserrat|Open+Sans:400,700,600',
        '/js/fancybox/jquery.fancybox8cbb.css?v=2.1.5',
        '/js/mediaelement/mediaelementplayer.css',
        '/css/settings_style.css',
        '/css/style-print.css',
        '/css/skin_style.css',
        '/css/style.css',
        '/css/color_cheme.css',
        '/css/responsive.css',
    ];
...

They are added like this:
<link href="http://fonts.googleapis.com/css?family=Patua+One|Montserrat|Open+Sans:400,700,600" rel="stylesheet">
<link href="/js/fancybox/jquery.fancybox8cbb.css?v=2.1.5" rel="stylesheet">
<link href="/js/mediaelement/mediaelementplayer.css" rel="stylesheet">
<link href="/css/settings_style.css" rel="stylesheet">
<link href="/css/style-print.css" rel="stylesheet">
<link href="/css/skin_style.css" rel="stylesheet">
<link href="/css/style.css" rel="stylesheet">
<link href="/css/color_cheme.css" rel="stylesheet">
<link href="/css/responsive.css" rel="stylesheet">

The problem is that:
<link href="/css/style-print.css" rel="stylesheet">

Should be:
<link href="/css/style-print.css" type="text/css" media="print" rel="stylesheet"/>

I tried to register this file separately in head.php:
$this->registerCssFile('/css/style-print.css', [], 'media="print"');

But this error is thrown:
in F:\OpenServer\domains\portf.ru\vendor\yiisoft\yii2\helpers\BaseHtml.php
Illegal string offset 'rel'

// показывая на такой вот код 
* @see url()
     */
    public static function cssFile($url, $options = [])
    {
        if (!isset($options['rel'])) {
            $options['rel'] = 'stylesheet';
        }
        $options['href'] = static::url($url);
        return static::tag('link', '', $options);
    }

At the same time, it works simply:
$this->registerCssFile('/css/style-print.css');
Please tell me how to add attributes for specific css files and why
registerCssFile does not work with additional attributes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Morozov, 2014-03-06
@nepster-web

As for the bundle attributes, only options for the entire bundle are currently implemented, see the $cssOptions property.
Accordingly, either take it out to a separate asset with the establishment of a dependency, or manually, through registerCssFile.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question