dongzhang0418 2018-07-27 15:26
浏览 34
已采纳

仅链接正在使用的组件的样式表

I am building a page with PHP and am making heavy use of templating to split the various parts of each page up into reusable components, each of which has its own stylesheet which is automatically added to the head by script.

However, to save bandwidth and avoid style conflicts in certain situations, I would like to only link those stylesheets which are actually used on a given page. Since the head goes first in the page and the styles go in the head, but there's no way to access something like get_included_files() until the end of the page, I'd (presumably) need a way to reach back into the head and add the link tags later, but I'm not sure how to do that.

The only obvious way I can think of is to necessitate a registry array of all the components which will be used, and then use that to generate the link tags, but that is not really viable because some of the components include other components, so it would be necessary to manually register not just the components included in the top-level file, but all the components included by those components as well.

  • 写回答

1条回答 默认 最新

  • doujianwan7570 2018-07-27 18:28
    关注

    I figured out a solution. I took each of my components, and basically wrapped them in a class and a public static function of that class, called output(). I can now include() all the components I want to use right at the start, and then just do Component::output() wherever I want it to appear. This way, all the files are included, and visible to get_included_files(), by the time I call output() on the script that generates the head.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM