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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图