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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include file question 1

Status
Not open for further replies.

73pixieGirl

Programmer
Oct 3, 2006
65
US
I'm using CSS and XHTML...if I wanted to use the same header and menu on multiple pages, is it just a matter of changing the file extension of my main page and the extension of my include file to .shtml, then putting the include statement (<!-- #include virtual= -->) in a div tag?
Thanks in advance!
 
You have to ensure that your web server supports them as well (not all web servers do)

Then the syntax I always use is something similar to this:

Code:
<!--#include File="blah.inc"-->

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Thanks kaht! What is the different between 'include file' and 'include virtual'?
 
According to this site (
include virtual is used if the included file is in a different directory, and include file is used if the included file is in the current directory

[small](seems silly to me to have 2 different commands for something that should be supported by 1 command)[/small]

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
There are some useful resources on SSI at faq253-3309

My understanding is that the [tt]file[/tt] directive is applied to the file system of the server, so you could use it to include files that are outside your webspace. For example, if your home page lives in a directory called /home/myuser/public_html, you could do this:
Code:
<!--#include file="/home/myuser/secure/blah.inc"-->
which would stop anybody gaining direct access to blah.inc over the web. However, SSI is often configured to disallow the use of this kind of file directive as it can present a security risk. What if somebody injected this into one of your pages somehow?
Code:
<!--#include file="/etc/passwd"-->

The [tt]virtual[/tt] directive, in contrast, identifies files in relation to your web root directory - i.e. the directory that your home page is in. Included files must be in your web space, and could be accessed individually if people know (or guess) the URL. That's not really a big problem though.
[...] then putting the include statement (<!-- #include virtual= -->) in a div tag?
Firstly, SSI is fussy about whitespace - don't put any spaces between the [tt]<--[/tt] and the [tt]#include[/tt] or it won't work.

Secondly, there's no reason to put the SSI inside a <div> element, in fact it's a bad idea. What you have to remember is that when the web server is dealing with SSIs, it's just sticking text files together, it doesn't care about HTML. What matters is that the document produced by that process is properly marked up HTML - it doesn't actually matter to the browser which bits come from the actual file and which from the SSI.

Here's the approach I'd recommend for using SSI in your pages.

On most websites, all pages conform to this pattern:
Code:
<html>
<head>
[i]Some elements specific to this page, eg <title>[/i]

[i][red]Some elements common to all pages, eg a stylesheet <link>[red][/i]
</head>
<body>
[i][red]Some elements, or part elements like an opening <div>, that are common to all pages[/red][/i]

[i]The actual unique content of the page[/i]

[i][red]Some elements, or part elements like a </div>, that are common to all pages[/red][/i]
</body>
</html>
I use three SSI includes to hold the three common (red) areas listed above. This technique, and its benefits, is expounded at more length in thread828-1086345 in my post dated 27 Feb 06 .

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Thanks for the information! Looks like I have a lot of reading to do! :)
 
While you're at it, look into SSI variables. They can make the process even simpler so you can set some variables to define the page-specific stuff and your page template can be 6 or 8 lines plus your content:
Code:
<!--#set var='v_pagecolor' value='blue' -->
<!--#set var='v_pagename' value='This is my page Title' -->
<!--#include virtual="/lib/header.html"-->
<!-- Content starts here -->
.
.
.
<!-- Content ends here -->
<!--#include virtual="/lib/footer.html"-->
HEADER.HTML contains everything from the top down through and including your logo or banner, and FOOTER.HTML contains stuff like 'page last updated' (which is also a SSI function called #FLASTMOD) and copyright info. I also put my navigation in FOOTER.HTML to make the rendered page easier to read.

For example, you can set the title to the value of the variable using:
Code:
<title><!--#echo var='v_pagename' --></title>
and display it in the content ares the same way:
Code:
<h1><!--#echo var='v_pagename' --></h1>

Probably more than you want to know - but SSI can be VERY useful if your web server supports it.

One fact to remember that Chris alluded to but didn't emphasize : Files that contain SSI directives (the "<!--#" lines) must have the .SHML extension, but the files referred to in the includes (like HEADER.HTML in my example) don't need to, unless they also contain SSI directives.

Enjoy!

Mike Krausnick
Dublin, California
 
Thanks for that, Mike. I've not used SSI variables myself, but it's another tool to put in the box. Do you know if their values can be accessed when the included file is an executable file rather than a flat one? I tend to use perl programs to generate my includes.

If you don't fancy renaming all your files to .shtml, you may be able to use a .htaccess file to enable it for other extensions. Just find or create a file in your root directory called .htaccess and put this line in it:
Code:
AddHandler server-parsed .htm

There's more on SSI (and .htaccess for that matter) at . It's written by a guy that used to post a lot on here (are you still out there cian?)

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
it's also worth noting that depending on your web hosting you can associate htm/html extensions with SSI , so what would appear to search engines and alike as static pages are actually dynamic ones using SSI.

I have access via my PLESK admin page to set this myself with my current host so was real easy to set up.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I've never used Perl, but the SSI variable is visible, and if perl EXEs can accept hard-coded parameters, then one might be able to pass the value of a SSI variable just as one would pass a hard-coded value, by placing the #echo directive at the parameter insertion point.



Mike Krausnick
Dublin, California
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top