Skip to content Skip to sidebar Skip to footer

Possible To Have "inheritance" In Html?

Let's say all of my html pages will have a top bar and banner with the same content. Rather than copy the code for these content on all html pages, is it possible to have pages in

Solution 1:

In this situations (as far as I know)

  1. You can use template based editors like Dreamweaver
  2. You can use framesets (don't use them)
  3. You can use iframe (meh.)
  4. You can convert your files to PHP and just use a single include command (Y)
  5. Copy and paste whole thing and when you get 100 pages, try to add a new menu...

I'd like to see other solutions too.

Example:

Lets say I've created a template.html it's something like

<html><head><title>asd</title>
    style tags keywords bla bla bla 
   </head><body><divclass="menu">
      yeah I've my menu here well designed
   </div><divclass="content">
      unique content here
   </div></body></html>

Allright this is my one html file. Lets take top section of the page. Menu will be same but content will be changed so this is top of the page:

<html><head><title>asd</title>
    style tags keywords bla bla bla 
   </head><body><divclass="menu">
      yeah I've my menu here well designed
   </div>

Save this part as top.php Now let's see what have we left:

<divclass="content">
          unique content here
       </div></body></html>

This will be our post page. But how can we get codes from top.php? Just like that:

<?phpinclude("top.php"); ?><divclass="content">
          unique content here
       </div></body></html>

Now, save this as page1.php BINGO! You did not wrote anthing about menu but include method will bring it for you.

Include basically writes everything from a file to another. You can check differences for include_once, require, require_once too.

Allright, we've created our first page. What about second one? Exactly the same:

<?phpinclude("top.php"); ?><divclass="content">
          my second page here
       </div></body></html>

Save this as page2.php

Well, you need to change your menu now but there are two pages, two hundred pages, two million pages... Who cares. Just change top.php that's all.

Please note that in this codes; top.php, page1.php and page2.php are in SAME directory. If you want to include from another path, you must use for example:

<?phpinclude("../top.php"); ?>//OR<?phpinclude("myFiles/theme/top.php); ?>

depending on your path.

I hope this helps. Read PHP guides for include. It's really easy.

You need a testing server (or you can use a local server like WAMP, XAMP etc.) to execute PHP files.

Solution 2:

You can create template page and include it to the all new pages as javascript

<script type="text/javascript" src="includes/template.js"> </script>

Same way with php - using include

Post a Comment for "Possible To Have "inheritance" In Html?"