fastadmin正则替换{__STYLE__}与{__SCRIPT__}为当前html模板文件中所写样式与js效果

www.jswusn.com PHP 2024-05-25 11:50:39 47次浏览

  苏州网站建设在fastadmin框架制作的插件个人中心样式时发现网站模板中使用了{__STYLE__}与{__SCRIPT__}被原样输出了,这不对劲啊,这原样输出不就是没解析模板么,我翻找了一下官方的cms找到了解决方法,以下为修改步骤:



在前端控制器的公共控制器中增加以下代码:

public function _initialize()
{
    parent::_initialize();
    Hook::add('view_filter',function (& $content){
        $style = '';
        $script = '';
        $result = preg_replace_callback("/<(script|style)s(data-render="(script|style)")([sS]*?)>([sS]*?)</(script|style)>/i", function ($match) use (&$style, &$script) {
            if (isset($match[1]) && in_array($match[1], ['style', 'script'])) {
                ${$match[1]} .= str_replace($match[2], '', $match[0]);
            }
            return '';
        }, $content);
        $content = preg_replace_callback('/^s+({__STYLE__}|{__SCRIPT__})s+$/m', function ($matches) use ($style, $script) {
            return $matches[1] == '{__STYLE__}' ? $style : $script;
        }, $result ? $result : $content);
    });

    
}


技术分享

苏南名片

  • 联系人:吴经理
  • 电话:152-1887-1916
  • 邮箱:message@jswusn.com
  • 地址:江苏省苏州市相城区

热门文章

Copyright © 2018-2024 jswusn.com 版权所有

技术支持:苏州网站建设  苏ICP备18036849号