-1

I have a static website with lots of similar pages. The site is very simple, basically just some information for me and a few other people. I'd basically like to spend as little time as possible maintaining it.

Certain pieces show up on every page, and change every so often (the header and footer, for example, but other similar pieces are repeated too). I currently copy-paste them on every page they appear on, but this obviously is error-prone and is very frustrating for me to maintain.

My initial thought was JS to load similar pieces of the site, but this feels like overkill for such a simple site, and makes reading the site offline (in a text editor or terminal for example) a giant pain because one has to jump between different files.

My second thought was to just use the C preprocessor to generate the HTML files, for example doing #include "repeated_section.html" and a few macros to build up the HTML from pieces. This seems to work well, (the generated HTML works, and is nice and readable in a text editor), but feels kind of hackey. Is there a "proper" tool for doing this? Has anyone tried my way and run into any problems?

closed as off-topic by Becuzz, Jörg W Mittag, amon, Robert Harvey Mar 22 at 19:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to find or recommend tools, libraries, programming languages, resources (including books, blogs, tutorials, and examples), or projects to undertake are off-topic here as they attract opinionated answers that won't have lasting value to others. You may be able to get help in The Whiteboard, our chat room." – Becuzz, amon, Robert Harvey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    There are many, many, many static site generators and template engines specifically for your use case. The C preprocessor is horrible here because it still tries to tokenize your HTML as if it were C, in order to apply any macros. Note that GH pages has built-in support for the Jekyll static site generator. – amon Mar 22 at 18:36
  • @amon, yes, I realize the C preprocessor is not ideal for this use case, which is why I asked this question in the first place – CoffeeTableEspresso Mar 22 at 18:39
  • downvoter, care to explain? – CoffeeTableEspresso Mar 22 at 18:46
  • Use a static site generator. Gatsby with React seems popular. – Esben Skov Pedersen Mar 23 at 16:40
3

Your web-server itself can do this with Server Side Includes.

Use the include directive in your page to insert a header and/or footer file.

<!--#include virtual="/footer.html" -->

Sadly, this doesn't work with GitHub Pages, but they do have a similar feature.

from Configuring GitHub Pages

GitHub Pages can include other files, similarly to SSI or PHP includes, but in this case the files to be included must be located in a top-level subdirectory called _includes.

{% include nav.html %}
  • I forgot to mention, I have nothing server side, it's all hosted with GH pages. Would this still work? – CoffeeTableEspresso Mar 22 at 18:34
  • @CoffeeTableEspresso No, this depends on the server (like Apache). – amon Mar 22 at 18:37
  • It also works for nginx and IIS – toastifer Mar 22 at 18:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.