Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which naming convention is correct for included HTML code fragment files?

I do have an index.php file which includes different parts of the page via PHP.

For example this includes the <head> section of the HTML page into the index.php file:

<!doctype html>
<html>
    <head>
        <?php include($_SERVER['DOCUMENT_ROOT']."/PATH-TO-FILE/head.php"); ?>
    </head>

    <body>
        ...
    </body>
</html>

The content of "head.php" may be something like this:

<meta charset="utf-8">
<title>Title of page</title>
<meta name="description" content="Description text...">

...

<link rel="stylesheet" href="stylesheet.css">

...

and so on...

Technically "head.php" is not a PHP file because it does not contain any PHP code. Neither is it a valid HTML document. It is just a HTML code fragment.

Until now I have always named these HTML code fragment files either *.html or *.php.

My question is whether this is correct or not?

Is it advisable to not give it a file extension at all? Instead of "head.php" simply "head"?

Please keep in mind that certain directives can be set in the server's .htaccess file concerning the caching and expiration of *.html and *.php files. Removing the file extension and renaming it from "head.php" to "head" may exclude it from the mentioned directives in the .htaccess file.

While searching I found this question here on StackOverflow: What extension should I use for files containing fragments of HTML

But that question was asked 6 years ago and there are so many different file extensions mentioned there, it's difficult to say which one to use.

Can anyone give an updated answer concerning this issue?

like image 522
DSC Avatar asked Sep 07 '15 17:09

DSC


1 Answers

If you use include, I'd strongly recommend using *.php. include executes the code in it, even if it's raw HTML. (in that case it's just output without much further processing)

Hence, use *.php for files output that way. (Else you might get also bad highlighting in editors when using <?php one day in it for some reason; also, if someone opens the *.html file directly, he'll see the raw code then)

In case you are using e.g. readfile(), then use *.html to highlight that it is raw HTML code only, nothing ever will be executed.

It basically depends on how you include the file.

P.s.: To no extension at all. Not really advisable, that's usually what you use for binary files and directories. Note that extensions really just are to give you oversight what happens.

like image 161
bwoebi Avatar answered Oct 07 '22 05:10

bwoebi