drduh44480 2012-03-29 12:20
浏览 33
已采纳

如何包含html / php文件中的所有资产路径?

This is a bit hard to explain but I'll give it a go. Lets say I have this in the header of a HTML file called myFile.html:

<!doctype html>

<head>

<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/style.css" />
<script src="js/hovers.js"></script>
<link rel="stylesheet" href="css/highlight.css">

</head>

I now want to include this file in a php file, lets say index.php. However, all my assets (css, js, etc) are stored in a folder called assets/.

After including (or before?) this html file, is there a way to change all the asset paths to point to assets/*. For example, 'css/reset.css' would be changed to 'assets/css/reset.css' and so on. Note that this isn't just limited to these lines in the header, but also includes things like image elements, etc.

If that's confusing, let me know and I'll try explaining again!

Cheers :)

  • 写回答

1条回答 默认 最新

  • duanchuanqu593743 2012-03-29 12:31
    关注

    I usually define a constant for things like this, then it's easily changeable via a config file. It does mean going through your template files and adding this constant variable in, but after that it becomes really easy to change.

    So your file would be something like this:

    <?php
    require_once('config.php');
    include('header.php');
    

    config.php would be something like this:

    <?php
    define('ASSETS_ROOT','/assets/');
    

    and header.php would be like this:

    <!doctype html>
    
    <head>
    
    <link rel="stylesheet" href="<?php echo ASSETS_ROOT; ?>css/reset.css" />
    <link rel="stylesheet" href="<?php echo ASSETS_ROOT; ?>css/style.css" />
    <script src="<?php echo ASSETS_ROOT; ?>js/hovers.js"></script>
    <link rel="stylesheet" href="<?php echo ASSETS_ROOT; ?>css/highlight.css">
    
    </head>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备