Search code examples
phphtmlrequire-once

Is there a HTML variant of require_once?


In HTML is there such a line of code that will do the same thing as PHP's require_once? I'm just curious because there are some lines of codes that I want to duplicate through multiples sheets without needing to require myself to type it each page.

I know I can do it via PHP, but I am looking for an HTML variant? Is there such a beast or am I barking up the wrong tree?


Solution

  • Yeah, SSI is the closest you're going to get. However, there are many non-server-side ways to get around this. Several web development applications have html templating systems that replicate server-side includes on the development side. For example, dreamweaver allows you to insert repeatable regions into HTML templates. When you modify the "included" file, Dreamweaver will modify all HTML files that use that block. As this is not a true include, but rather an HTML-updating system, you do have to then re-upload these files if you use a remote server, but if you have to stick to plain HTML it can make projects much more manageable and is much better than using iframes.

    Lastly, there is also the option of having Javascript build a repeating block of code. You can simply include a common javascript library on every page <script type="text/javascript" src="templater.js"></script> and have it build the element on the client's side (either with an innerHTML call or inserting elements into the DOM). This has the obvious downside that

    1. It requires Javascript to work
    2. It might mess with SEO
    3. It may slow down page loads (from the client side anyhow)

    Using a proper include in a server side language is of course the best approach, but in a pinch these are both potential alternatives.