chintanparikh
chintanparikh

Reputation: 1682

How do I change all the asset paths in a html/php file when including it?

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 :)

Upvotes: 0

Views: 659

Answers (1)

Nick
Nick

Reputation: 6346

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>

Upvotes: 1

Related Questions