Best practice to create a template in HTML

There are, essentially, three places you can do this:

  1. At build time. Run a script that puts the pages together and outputs static HTML. Then upload the static HTML to the server. ttree is great for this. You could wrap the script up in a complete build system that combines and minifies your CSS and JS and then uploads it to the server. This is the option that needs the least amount of worrying about what clients and servers support. Update: These days there are plenty of static site builders that incorporate this functionality.
  2. At runtime on the server. This could be a simple SSI, a PHP include or a full template system (of which there are many, including Template Toolkit and mustache), possibly running inside an MVC framework (such as Catalyst or Django). This is the most common approach, it has less time between making a change and putting the result life then doing the templating at build time.
  3. At runtime on the client. This adds a dependency on JavaScript, so I wouldn't recommend it unless you already had a server side solution in place.

There are a couple of ways to do this.

If you are working purely with HTML, then you can use a 'server side include' -- this is a tag which allows you to load a file into the page (see http://en.wikipedia.org/wiki/Server_Side_Includes). Whoever is hosting your website has to support this.

<!--#include virtual="../quote.txt" -->

If youe web host happen to use PHP, you can do this using the include method (see http://php.net/manual/en/function.include.php):

<?php include('path_to_file/file.htm');?>

This will include the file at the point you place the tag. But again, whoever is hosting your site needs to support this.

There are other methods of doing this, bit these are the two I make use of.

Tags:

Html

Templates