doucitan2544 2014-03-15 10:38
浏览 25
已采纳

如何在PHP的头文件中包含CSS? [关闭]

Lets say I have markup like:

<!DOCTYPE html>
  <html>
    <head>
    </head>
  <body>
     <?php
     PutIntoHeader("<script src="im_on_top"></script>")
     ?>
  </body>
</html>

The PutIntoHeader function would put the given script into the head section of the html which is rendered. I might be missing something really obvious, but I cant seem to figure that out. Any ideas how the function would be able to do that?

The output would be:

<!DOCTYPE html>
  <html>
    <head>
       <script src="im_on_top"></script>
    </head>
  <body>
  </body>
</html>

Thank you for any ideas.

Edit:->

Hi, I feel like a noobie getting this kind of answer:) My system is highly complex and in the time I write header I dont know which scripts will get requested for that particular page as its all dynamic. I could put all html in variable before it gets rendered and then traverse it as xml and put it there manually while storing the scripts in an array but I dont want to go this road.

  • 写回答

4条回答 默认 最新

  • duanhe4155 2014-03-15 11:27
    关注

    In almost all situations you can simply get away with combining and minifying you scripts into a single file. This slows down the initial loading time (first visit of site), but will speed up further visits / navigation because the file will be cached.

    Of course YMMV and it depends on your specific situation. See for example this thread.

    There is also the thing that user agents cannot do unlimited concurrent connections to the same domain to load resources.

    See for example these two (somewhat outdated) benchmarks:

    In the comments you said you are working with a million different widgets. Does this mean that these widgets all have completely specific and different code or is just the data different or presented differently? If it is just data / presentation you don't need different script for that.

    If indeed you are talking about a million completely different codes for the different widgets (a million suddenly sounds like a made up arbitrary number) it indeed may make sense to not load everything in a single script. But this is impossible to answer by us, because it depends on the filesize, how fast you want to load the first visit, how many shared code it has and more numbers like that.

    So to get back to your questions "How can I load specific scripts in the head?".

    One option would be to not render it in the head, but place it just before the </body> and body tag instead. The advantage of this approach is that you know what script to load by then. The pages will load faster (because resources in the head will be first downloaded in full before the actual page is ever going to be rendered). The drawback of this approach is that there is a small latency before your script will kick in because it will be last thing that gets loaded in the DOM.

    Another option would be to defer the rendering of you HTML on the PHP side and using it more like a templating engine. A simple example of this is by first getting all the widgets you want to display and only after you have done this start the HTML rendering in PHP.

    some bootstrap file

    <?php
    
    // load widgets from db or wherever
    $widgets = [
        [
            'template' => '/templates/widget1.phtml',
            'script'   => '/js/widget1.js',
        ],
        [
            'template' => '/templates/widget2.phtml',
            'script'   => '/js/widget2.js',
        ],
    ];
    
    require __DIR__ . '/templates/page.html';
    

    some template (page.phtml)

    <!DOCTYPE html>
    <html>
        <head>
            <title>My page with widgets</title>
            <?php foreach ($widgets) { ?>
                <script src="<?php echo $widgets['script']; ?>"></script>
            <?php } ?>
        </head>
        <body>
            <?php foreach ($widgets) { ?>
                <?php echo $widgets['template']; ?>
            <?php } ?>
        </body>
    </html>
    

    P.S. in the above example I have used script tags, but it could also be expanded for css files also obviously.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理