zoukankan      html  css  js  c++  java
  • Yii2 assets注册的css样式文件没有加载

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

    <?php
    
    namespace frontendassets;
    
    use yiiwebAssetBundle;
    
    class LayuiAsset extends AssetBundle
    {
        public $sourcePath =  "@frontend/assets/app";
    
        public $js = [
            'layer.js',
            'layui.js',
        ];
    
        public $css = [
            'css/layui.css'
        ];
    
        public $jsOptions = ['position' => yiiwebview::POS_HEAD];
        
        public $depends = [
            'yiiwebJqueryAsset',
        ];
    }
    

    但是,打开网页没有引入,发现目录下已经发布了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("
    ", $this->metaTags);
            }
    
            if (!empty($this->linkTags)) {
                $lines[] = implode("
    ", $this->linkTags);
            }
            if (!empty($this->cssFiles)) {
                $lines[] = implode("
    ", $this->cssFiles);
            }
            if (!empty($this->css)) {
                $lines[] = implode("
    ", $this->css);
            }
            if (!empty($this->jsFiles[self::POS_HEAD])) {
                $lines[] = implode("
    ", $this->jsFiles[self::POS_HEAD]);
            }
            if (!empty($this->js[self::POS_HEAD])) {
                $lines[] = Html::script(implode("
    ", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']);
            }
    
            return empty($lines) ? '' : implode("
    ", $lines);
        }
    

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

  • 相关阅读:
    python threading 无法并行问题
    flask
    jinjia2 模板不解析html
    docker 配置lnmp环境(mac环境下)
    docker基础
    django初体验
    Centos6.5 编译安装Mysql 5.5.3.7
    Mysql InnoDB事务
    删除GitHub上项目中的某个文件
    转 WPF MVVM 循序渐进 (从基础到高级)
  • 原文地址:https://www.cnblogs.com/yangxunwu1992/p/6044991.html
Copyright © 2011-2022 走看看