Does HTML support splitting source over multiple files? I'm looking for some equivalent of C++'s #include
; or maybe something like C#'s partial
; an element that could take source path and inject the file contents at that place.
Apologies if this has been asked before. Google and SO searches didn't return much. I'm not a web guy, but the only solution I found was using an iframe
, which many people do not like for various reasons.
It is just that my html source is becoming huge and I want to manage it by splitting into multiple files.
You can have multiple instances of the HTML Help ActiveX control in a single HTML file to split the file in multiple locations. This feature can be used only with a compiled help (. chm) file. The split HTML files do not appear in the [FILES] section of the project (.
You can use any modern browser to split HTML file, for example, Google Chrome, Microsoft Edge, Firefox, Opera, or Safari.
You can't, at least not in flat-HTML. What you can do is using Javascript to load and place the snippets. iframe
s are also non-ideal because contrary to what happens with directives like #include
and partial
, those snippets will never be compiled in one single page.
However, I think it's important here to understand how your pages will be served. Is this a static website? Because in this case I would write a simple script in your language of choice to compile the page. Let's say that you have a base like this:
<html>
<head>
<!-- ... -->
</head>
<body>
{{ parts/navigation.html }}
<!-- ... -->
</body>
</html>
You could write a script that runs through this file line by line and loads the content into a variable named, for example, compiled_html
. When it finds {{ file }}
it opens file
, reads its content and append it to compiled_html
. When it gets to the end, it writes the content of the variable into a HTML file. How you would implement it depends on the languages you know. I'm sure that it's pretty straightforward to do it in C#.
This way you'll be able to split the source of your HTML pages into multiple files (and reuse some parts if you need them), but you'll still end up with fully functional single files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With