36

The ".html" suffix on a filename implies that the document contains html, head, and body tags.

I have some files that each contains a div element or two, but no html or body tags. The file contents are well-formed HTML fragments in the sense that they could be inserted into a body tag of a compliant HTML document, and it would still be compliant. (They contain no "<% %>" markers, no PHP code, etc.) But a fragment file is not compliant HTML by itself, so I'd like to give it a different naming convention.

Several "file extension" sites include an entry for ".PHT" and describe it as "Partial Hypertext File." That sounds promising, but I can't find any additional explanation on the origin, expected file format, or applications that use it. Also, many of the same sites identify ".phtml" and ".phtm" (which appear to be longer versions of the .pht suffix) as PHP files — as noted, my files are not PHP files.

Should I use ".pht" as a suffix? Is there a more appropriate naming convention?

Edit: I'd like to distinguish fragment files from the full HTML documents in the same directory.

system PAUSE
  • 37,082
  • 20
  • 62
  • 59
  • How are you performing the insertion? – John Topley May 01 '09 at 15:35
  • 1
    @John Topley: In Javascript, setting the innerHTML of an Element after loading the fragment file via XMLHttpRequest. – system PAUSE May 01 '09 at 16:02
  • 6
    I'm a fan of .htmf (html fragment). I don't know of any standardization effort for this, but it would be nice if one caught on. – Lilith River Sep 18 '11 at 14:00
  • 1
    Unless you cannot have multiple dots in a file name or you want a custom file extension, you could use: `.part.html` (or something similar). Using an existing file extension makes the file recognized by text editors and web browsers by default (although registering a new extension with a program isn't hard to do). – Dave F Mar 24 '20 at 23:24

11 Answers11

16

I would use .inc (meaning include file) or .txt.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    +1 on .txt... because without the full valid structure .. it's just a text file. – datacop May 01 '09 at 15:44
  • 1
    <head>, <body> are HTML tags used to define sections of a document written in HTML. Without them the HTML doesn't describe a web page, but is still HTML. – Peter Perháč May 01 '09 at 15:52
  • @Daniel, I'm using HTML 4.01, not XHTML. – system PAUSE May 01 '09 at 16:09
  • @system - I was referring in general case. – Daniel A. White May 01 '09 at 16:12
  • http://msdn.microsoft.com/en-us/library/ms972100.aspx - Looks like there's a solid precedent for this, server-side at least. I'm not using ASP, but the difference between .html and .inc should be obvious to other coders who come from the ASP world. – system PAUSE May 01 '09 at 21:44
  • As indicated in other answers, .html (or related extension) has advantages. .html will have syntax coloring and run validation in editors. It doesn't bother me one bit that the fragments would not be perfectly formatted. Full file name should indicate the contents: "something.frag.html". Keep the files in a folder called "fragments" (or some such). No fuss. No mess. Using .txt or .inc or .php obscures the actual purpose. .txt is a nothing burger. The .inc could be any kind of include. We are the music makers. We are the coders of code. – horace Feb 10 '23 at 16:24
7

I generally use .inc or .tpl extensions.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
6

If selecting an extension for fragments it makes sense to pick an extension that code editors think is html so that you get all your highlighting goodness.

The Atom editor uses the following for html: .ejs, .htm, .html, .kit, .shtml, .tmpl, .tpl, .xhtml and the Scite editor uses: .asp, .cfm, .hta, .htm, .html, .htd, .jsp, .htt, .shtml, .tpl, and .xhtml. Select one used by both, and see if it is also defined by your editor of choice.

For templates I use .tpl and set the first line with a percent and exclamation mark:

%!
<followed by rest of file here>

because the %! causes Chrome and Firefox to treat a file as text by default.

robocat
  • 5,293
  • 48
  • 65
6

As the files are really written in Hyper Text Mark-up Language, I think it's quite valid to give them the .html extension. Consider just calling the Directory something like "divs" or "panels", "forms", etc...

Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
4

By definition .html or .htm are files that contain HTML regardless of version. There is no official specification the mentions they should contain full HTML document definitions, just they should contain HTML.

XHTML is a different ball game as it must contain validated content, therefore a full well-formed XHTML document is required.

I use .html and have organised the files with HTML fragments in a separate folder however my editor respects the fact they do not have to be full documents and doesn't produce any warnings.

To distinguish differences in the same directory you could use .htm for partial content and .html for full documents or vise versa.

Alternatively you could prefix the extension such as file.partial.html or file.p.html

If your editor produces warnings, it may be an idea to look into your specific editors reference material (help files, website, support forums, etc.) for a solution, that may in-fact be using another extension like .tpl or .inc.

Edit - Forgot to write about XML: HTML is XML so you could use the .xml extension, the caveats being:

  1. You probably won't get HTML type-hinting
  2. Valid xml files need <?xml version="1.0"?> as the first line so an include process may insert this line where its not wanted.
  3. Only one container object is supported, so you'd need one <div> or similar element containing all your content, so you couldn't have two <div> in the root of the document.
Steven Turner
  • 136
  • 1
  • 4
3

I generally use .inc (include file) or .shtml (shared HTML). I've never heard of .pht, though.

Matt Grande
  • 11,964
  • 6
  • 62
  • 89
  • @anonymouscoward yes, but Atom takes it as plain-olde HTML... which works for me! otherwise i'd use fhtml. Server Side Includes SHTML typically has templating features like including other shtml... which makes this perfect... as long as your server isn't too eager. – Ray Foss Sep 14 '16 at 19:45
2

I use ".i.html", which to me covers most of the bases, and is unique. Seems to work.

hen3ry
  • 66
  • 3
  • 1
    This would still be served as a "normal" page. By having a non .html file name, the access can easily be restricted and not served – James A Mohler Dec 04 '12 at 00:15
2

I've used:

  • .htm for fragment.
  • .html if its a full page.

Sometimes I think of using htmlf for fragments, as in JSP you have .jsp for full pages and .jspf for fragments.

igr
  • 10,199
  • 13
  • 65
  • 111
  • `.htm` is not a good option because OS's just treat it as an alternative to `.html`, so it would be expected that it's compliant HTML, but fragments are typically NOT compliant HTML. – Pete Dec 30 '19 at 17:43
2

I've only seen the ".phtml" extension before but I've never used it. I always use ".html", like any other ".html" file. Like MasterPeter said the file is written in HTML, so I think it's correct.

HED
  • 183
  • 4
2

Another extension, ".fhtml", appears to be used by some outdated Macromedia product, and at least one other software package uses it to mean "fragment HTML".

Having a precedent is nice, but unfortunately the .fhtml suffix also used to indicate HTML templates with embedded Factor code. That kind of ambiguity is annoying.

system PAUSE
  • 37,082
  • 20
  • 62
  • 59
1

I use "inc". I don't see any reason to worry about a thing like that. Call it whatever you want, as long as you get the benefit of looking at the files in a directory listing and not being confused.

One example where I don't use "inc" would be when there are includes inside includes, say your outer page includes a nav include, which then includes a third file. In that case you have to stay with whatever your server expects the extension to be for server-parsed files.

AmbroseChapel
  • 11,957
  • 7
  • 46
  • 68