Yii2 assets注册的css样式文件没有加载

准备引入layui.css文件的,在LayuiAssets类中已经配置了资源属性

<?php

namespace frontend\assets;

use yii\web\AssetBundle;

class LayuiAsset extends AssetBundle
{
    public $sourcePath =  "@frontend/assets/app";

    public $js = [
        'layer.js',
        'layui.js',
    ];

    public $css = [
        'css/layui.css'
    ];

    public $jsOptions = ['position' => \yii\web\view::POS_HEAD];
    
    public $depends = [
        'yii\web\JqueryAsset',
    ];
}

但是,打开网页没有引入,发现目录下已经发布了css样式文件,原来yii2 在加载css 的资源文件时,会注册时会生成对应的link标签,但是还未加入网页中,引入文件是通过layout中生成一个占位符常量,例如

const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';

然后通过strtr函数对占位符进行替换,换成对应的的代码:

public function endPage($ajaxMode = false)
    {
        $this->trigger(self::EVENT_END_PAGE);

        $content = ob_get_clean();

        echo  strtr($content, [
            self::PH_HEAD => $this->renderHeadHtml(),
            self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
            self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
        ]);

        $this->clear();
    }

renderHeadHtml这个方法时在页面结束的时候进行占位符替换,将头部占位符换成成对应注册的css和js代码。如下是生成link标签的函数

    /**
     * Renders the content to be inserted in the head section.
     * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.
     * @return string the rendered content
     */
    protected function renderHeadHtml()
    {
        $lines = [];
        if (!empty($this->metaTags)) {
            $lines[] = implode("\n", $this->metaTags);
        }

        if (!empty($this->linkTags)) {
            $lines[] = implode("\n", $this->linkTags);
        }
        if (!empty($this->cssFiles)) {
            $lines[] = implode("\n", $this->cssFiles);
        }
        if (!empty($this->css)) {
            $lines[] = implode("\n", $this->css);
        }
        if (!empty($this->jsFiles[self::POS_HEAD])) {
            $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]);
        }
        if (!empty($this->js[self::POS_HEAD])) {
            $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']);
        }

        return empty($lines) ? '' : implode("\n", $lines);
    }

但是并没有引入,如果需要引入,需要生成占位符即可,所以之前的css未引入的问题,只要在layout的头部添加一个<?= $this -> head() ?>就好了。

posted @ 2016-11-08 22:30  xnuwu  阅读(1194)  评论(0编辑  收藏  举报