r/HTML • u/Dauntle55Duck • 26d ago
Question Menu code repetition
Should I look for a way to call an external html page with the code for my menu? I feel like adding the code to every page might clutter the code a bit. It’s been a while and I don’t remember wether or not we discussed this in school lol
6
Upvotes
3
u/heesell 26d ago
Perhaps you could expand your question.
In frameworks, we use components for this but I found this code pen that does it in the same kind of way using Javascript: https://codepen.io/grace_2241/pen/XWXxWgr
2
u/glandix 26d ago
DRY: don’t repeat yourself … in other words, reduce duplicated code so when you need to update it, you change it in one place and you’re done.
7
u/azangru 26d ago
Yes. This means either running a server that generates the full html of a given page out of reusable pieces, or building the full html pages with a static site generator.
3
u/[deleted] 26d ago
Server-side includes have been around since the 90s for just this reason.
2
u/5eeso 26d ago
The problem you’re describing is one of the reasons static site generators exist. You can use something like Eleventy with Nunjucks to create a shared layout that each page can use.
2
u/Haasva 26d ago
Load with JavaScript using templates (very simple code) or use iframe as someone recommended. Two good options.
2
u/cshaiku 22d ago
PHP is exceptionally good at generating html templates.
2
u/Weekly_Ferret_meal 26d ago
OR if the option of servers or JS isn't there. you can go old school and use iframe.
the index.html contains the main page with the menu that targets the a content iframe to load the pages like home.html, about, contact, etc...
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe_target
That's how I built my first website.
It's old school, but it's still valid.