Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Set Up Common Code in HTML 8

Status
Not open for further replies.

xyzuser

Technical User
Aug 13, 2008
96
US
Basic question. I have HTML code that is common to many pages. Naturally I want to use these as a common routine that is callable, so that when I change the common HTML code it gets picked up in each page (instead of chaning each page individually).

Where do I store this and how do I link to/call it? It's not CSS styles and it's not javascript (I know how to do those).
Thanks
 
Short answer is you can't.

Long answer is yes, but not with HTML alone.
You see HTML is a static language and as such can;t do complex dynamic things such as calling content form other locations.

Usually for this you employ Javascript or server-side includes, or even a programmable server-side language such as ASP or PHP.

So in other words, its something you can do with the aid of other web development techniques.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you (and a star) vacunita, for explaining that. I'm running on a Linux server so I'll look into using PHP for this.
 
I thought I knew how this worked but I realize that I don't. Novice question: Can I just stick the HTML code within a PHP file and INCLUDE that PHP file?
 
You do not need to have the included files as php. Those (if they contain no php code) should be left as html, to not send them needlessly through the php interpreter. In a master file, which will have to be of php extension (unless you want to fiddle with which extensions get sent to the php interpreter), you will include the other files by opening and closing php tags and issuing include call.
Code:
html....
<?php
include "myFile.html";
?>
more html...

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
I use SSI for nearly every site I code, it makes including generic menus , footers , etc. a piece of cake!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Thank you very much for this help (and a star), Vragabond. I'm afraid something is still not clear to me: you said I shouldn't needlessly send files through the php interpreter. 90% of each page's html code is unique; about 10% of each page's html code is common to all of the pages. Given that, it seems I have to change each "master" page (which contain the 90% unique code) from .html to be .php files in order to be able to have
Code:
 <?php
include "myFile.html";
?>
to include the 10% common code (myfile.html). So 90% of my html code will wind up being processed by the php interpreter, which is what you said to avoid.
Have I misunderstood how this would work?

ChrisHunt
Thank you (and a star) for your suggestion. It sounds like a good solution. I'm a novice regarding SSI. But I thought I read somewhere that to use SSI you have to change all of the .html pages to be .shtml which would confuse visitors or use some conversion file to keep them as .html . Am I mistaken and/or confused about this?

And thank you (star), 1DMF for your personal experience in seconding the SSI approach
 
What he meant was the content of the included files, does not necessarily need to be run through the PHP parser. That is if say all you are including is HTML without any PHP then the file does not need to end in php.

The file that is calling the included file should be PHP, but here's where I think you have it backwards. The common code, should be your template page, and it should call in the other pages which have the unique content. Meaning you have one single PHP page, and the rest is just HTML.

The way you are including your common code in all pages kind of defeats the purpose of using includes. Since you end up having the same code on all pages. When really you should have a single page that calls in the unique content.

I don't know if any of that made sense, but if not I'll try to illustrate better if you want me to.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

xyzuser said:
But I thought I read somewhere that to use SSI you have to change all of the .html pages to be .shtml which would confuse visitors or use some conversion file to keep them as .html .
Is the same thing as with PHP : you have to configure your web server to pass files with certain extension through the interpreter.

The common extension for files containing PHP code is .php, for files containing SSI directives is .shtml . But you can configure your web server to pass .html files to the interpreter. That way the extensions are not changed, but every regular document will be searched for interpretable codes.

Feherke.
 
I'm a novice regarding SSI. But I thought I read somewhere that to use SSI you have to change all of the .html pages to be .shtml which would confuse visitors or use some conversion file to keep them as .html . Am I mistaken and/or confused about this?

If your pages are already indexed as .HTML and you cannot easily mod rewrite or 301 redirect the pages if changed to .SHTML (the standard extention for SSI).

If you have access to the web server (as I do) you can set the SSI interpreter to process HTML files, that way you get the power of SSI without the headache of redirecting all your pages.

I've also had a case where the web host tech support was able to make the change for me after a 10 secoond phone call, so even 3rd party hosted websites, can still have HTML passed through the SSI interpreter.

I've not noticed any performance issues having all HTML parsed in this manner even if it doesn't contain any SSI includes!

check it for yourself , all pages are .HTML but use SSI for menu, header & footer parts of the page ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

If you not want to mess with extension changing and anyway you need nothing dynamically generated, you can do the inclusion offline. For example with m4.

For example have this files in a directory :
[ul]
[li][red]one.html[/red], [blue]two.html[/blue] - separate documents[/li]
[li][green]menu.html[/green] - common piece for all documents[/li]
[li]make.sh or make.bat - script or batch file containing the processing commands[/li]
[/ul]
Code:
[red]<html>
<body>
<h1>One</h1>[/red]
include(menu.html)
[red]<p>First page</p>
</body>
</html>[/red]
Code:
[blue]<html>
<body>
<h1>Two</h1>[/blue]
include(menu.html)
[blue]<p>Second page</p>
</body>
</html>[/blue]
Code:
[green]<ul>
<li><a href="one.html">One</a></li>
<li><a href="two.html">Two</a></li>
</ul>[/green]
Code:
mkdir final
m4 one.html > final/one.html
m4 two.html > final/two.html
You just run the make.sh or make.bat file and it will create the following files in a directory called final :
Code:
[red]<html>
<body>
<h1>One</h1>[/red]
[green]<ul>
<li><a href="one.html">One</a></li>
<li><a href="two.html">Two</a></li>
</ul>[/green]
[red]<p>First page</p>
</body>
</html>[/red]
Code:
[blue]<html>
<body>
<h1>Two</h1>[/blue]
[green]<ul>
<li><a href="one.html">One</a></li>
<li><a href="two.html">Two</a></li>
</ul>[/green]
[blue]<p>Second page</p>
</body>
</html>[/blue]
Then you just publish the content of the final directory.

Note that the make.sh or make.bat file can be enhanced as you need.

Feherke.
 
I take it it only works on an IIS server feherke, though if you have Windows XP you can install a single website IIS version from the Win XP disks.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
make.sh or make.bat

I assume these are Web Server scripts for merging files?

Your latest example has lost me a bit

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

1DMF said:
I assume these are Web Server scripts for merging files?
Aha ! Got it.

Nope. That should be executed from the command line. ( It contains a list of commands that you could execute one by one from the command line. )

I should put more accent on the word "offline". I mean, you do the processing without any web thing. It is a simple text file processing : composes the desired documents from kind of template files.

So the web server will still serve static HTML documents, but you not have to modify them separately if you change something in the menu. Just edit the menu.html then execute the make.sh or make.bat to re-generate the final documents.

( Of course, probably most of the popular web servers could be configured to do this processing on the fly. But I never heard about anyone actually doing it. )

Feherke.
 
wouldn't using server side processing be it SSI or PHP , ASP or even PERL templating , be more preferrable than having to re-generate every page again on a website when you change something in the menu.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
horses for course i guess :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Thank you, feherke, for joining in and helping me (a star for you).
As I said, I am a novice and you are the experts. So what I am saying may still not make any sense. But this is how I understand what you've said would apply to me.

I have three categories of pages: Index, table of contents and 40+ individual pages. The index and table of contents have just a little bit of the common code in them and for simplicity I'll eliminate them from the discussion (I'll handle their common code updates individually). The other 40+ pages can be addressed either directly through the table of contents or serially from one page to the next.

Let's call my pages myfileA, myfileB etc. These files have common code in the beginning, the end and a couple of places in the middle. The beginning and end would not be a problem as the "INCLUDE" would be placed in between the "beginning common code" and the "end common code". Simple enough.

But for the common code in the middle, it would seem I need to break up each html file into a few files in this structure:
myfileA.php (or myfileA.shtml) - within this file would be:
Code:
[i]
common code [/i][b][COLOR=blue]<?php include (or SSI #include) "myfileApart1.html"; [/color][/b] [i]common code [/i][b][COLOR=blue]<?php include (or SSI #include) "myfileApart2.html"; [/color][/b] [i]common code [/i]
.

The same breakup and structure would be true for myfileB - myfileB.php (.shtml) , myfileBpart1.html, myfileBpart2.html - myfileC... etc.

And the good suggestion to have the single "common" file calling the unique code files would not seem to be work, either, as each of the 40+ unique pages can be addressed directly by the visitor. A common page would not know which real unique page the visitor wants to go to. So each page (myfileA, myfileB etc.) needs the common code (as above) and I couldn't have just one "master" common code page calling the unique code pages.

Again, as a novice, I may be all wrong and have misunderstood what you were all good enough to post.

And again, thanks to everyone for your suggestions, ideas and examples.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top