Reusing code?

B

Brett Baisley

I have a web page that has html code that I would like to add to every page
dynamically, instead of copying and pasting it to every page. That way, when
it needs to be changed, it will be a lot less work.

What is the best way to do this?

Thanks

Brett
 
S

SpaceGirl

Brett Baisley said:
I have a web page that has html code that I would like to add to every page
dynamically, instead of copying and pasting it to every page. That way, when
it needs to be changed, it will be a lot less work.

What is the best way to do this?

Thanks

Brett


Server Side Inlcude (SSI). Depends if your host supports it, and what
language it supports.

In ASP you'd do this...

<!--#include file="myfile.inc" -->

.....where "myfile.inc" can be any file within your website.
 
S

SpaceGirl

SpaceGirl said:
Server Side Inlcude (SSI). Depends if your host supports it, and what
language it supports.

In ASP you'd do this...

<!--#include file="myfile.inc" -->

....where "myfile.inc" can be any file within your website.


Actually you can include scripts this way too. You could for example have a
page written in ASP include another ASP script at the top; for example for
browser checking or whatever. Any server side scripts will get executed,
even if they are "included".
 
B

Brett Baisley

Any way to get this to work with just pure HTML? I found that I can include
a html file into another one, but then I'd have to change it to .shtml,
which doesn't work well here for some reason.
 
S

SpaceGirl

Brett Baisley said:
Any way to get this to work with just pure HTML? I found that I can include
a html file into another one, but then I'd have to change it to .shtml,
which doesn't work well here for some reason.

To be honest I dont know! When it comes to server side scripting, I only
know ASP. I'm pretty sure HTML is limited for this sort of thing; it's
totally reliant on server technology to pre-assemble your content before
it's sent out to the browser.
 
S

Steve Pugh

M

m

Brett said:
Any way to get this to work with just pure HTML? I found that I can
include a html file into another one, but then I'd have to change it to
.shtml, which doesn't work well here for some reason.

Preprocessing is also an option. Any program that concatinates files
will do, or you can use a more elaborate program.

Under 'nix you can simply write a script with a number of calls
similar to this, using cat and redirecting the pieces to a
final html file with a karet:

cat header.txt head.txt menu.txt body500.txt footer.txt > 500.html

Similar things can be done under 'doze.

It only takes moments to re-make a complete set of .html files, and
then you upload them while you have a cup of coffee and pat yourself
on the back.

If you know a little perl, you can write a preprocessor using the
File::Find module that will search and replace whole directory trees
in just a few lines of code.
 
P

Paul Furman

m said:
Preprocessing is also an option. Any program that concatinates files
will do, or you can use a more elaborate program.

Under 'nix you can simply write a script with a number of calls
similar to this, using cat and redirecting the pieces to a
final html file with a karet:

cat header.txt head.txt menu.txt body500.txt footer.txt > 500.html

Similar things can be done under 'doze.


Yep, in windows, you can make a text file named myfile.bat that contains
simple dos commands, put it in the folder you want to process & click
it. I forget what joins files but a > sends output to a new file like:

dir *.html > htmlist.txt

put that in a bat file & click it. Whatever the join commmand is, let's
pretend it's [join]...so in a folder full of pages, with a subfolder of
common header & footers:

[join] common\header.htm *.htm common\footer.htm > results\*.htm

or something like that <g>

it's easy to just make it a dopey list with cut & paste too:

[join] common\header.htm page1.htm common\footer.htm > results\page1.htm
[join] common\header.htm page2.htm common\footer.htm > results\page2.htm
[join] common\header.htm page3.htm common\footer.htm > results\page3.htm
[join] common\header.htm page4.htm common\footer.htm > results\page4.htm
[join] common\header.htm page5.htm common\footer.htm > results\page5.htm
 
P

Paul Furman

Steve said:
...If your server doesn't support any sort of include at all (so no SSi,
PHP, ASP, etc., etc.) then you can either move to a better host...


I'd recommend PHP over (what I understand to be Microsoft based ASP type
solutions). It's relatively easy & can do a heck of a lot more than just
server side includes. You don't need to learn it all and it's free
software so it doesn't cost much as an extra service. You can find bits
of code for free for doing things like an email contact page, guest
book, etc. all the way to about the most sophisticated thing you can
imagine with a MySQL database (also open source software but extra
charge to host). PERL is a lot more difficult to learn and use than PHP.
 
D

Davmagic .Com

From:
(e-mail address removed)
(Brett Baisley)
I have a web page that has html code that
I would like to add to every page
dynamically, instead of copying and
pasting it to every page. That way, when it
needs to be changed, it will be a lot less
work.

Bottom line is... get with a host that supports SSI and hopefully uses
Apache servers.... then you can take full advantage of using includes in
your pages, and you can set up an .htaccess file so that you don't have
to rename your pages to end in .shtml

For more info, see SSI on the Menu on this page:
http://davmagic.com/PAGES39.html

And look here to learn how to set up and configure an .htaccess file for
SSI:

http://www.outfront.net/tutorials_02/adv_tech/htaccess_magic1.htm

Web Design-Magic-Painting-Junking-Games
http://www.davmagic.com
Paint A House
http://www.paintahouse.com
Note: My email is ALWAYS Full to avoid SPAM... you can Contact me via my
websites...
 
T

Toby A Inkster

Paul said:
PERL is a lot more difficult to learn and use than PHP.

I dunno. Perl isn't really that hard to learn. And once you understand the
basics, it's very easy to solve big problems with just a few lines of Perl.

I have written a graphical jukebox to play OGG files in 227 lines of Perl
(excluding blank lines and comments).

Simple Perl CGI example:


---------------------------------------
#!/usr/bin/perl

$date = `date -R`;
$message = 'Hello World';

print <<EOF;
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>CGI Example</title>
<p>The date is <b>$date</b> and my message is <i>$message</i>.</p>

EOF
 
M

m

Toby said:
I dunno. Perl isn't really that hard to learn. And once you understand the
basics, it's very easy to solve big problems with just a few lines of
Perl.

Right. In general, Perl is easier to use than PHP after a short initial
learning period. It blows away C++ and Java for any kind of
administrative task. I think beginners panic seeing succinct markup like
'$_' and '@_' ; but it saves so much irritating typing,
and is actually not difficult to learn.

And, after all the work w3c has done getting display and content
seperated, why would anyone want to re-mud the waters by adding
a bunch of coding into the middle of an (X)HTML page?
 
S

SpaceGirl

Toby A Inkster said:
I dunno. Perl isn't really that hard to learn. And once you understand the
basics, it's very easy to solve big problems with just a few lines of Perl.

I have written a graphical jukebox to play OGG files in 227 lines of Perl
(excluding blank lines and comments).

Simple Perl CGI example:


---------------------------------------
#!/usr/bin/perl

$date = `date -R`;
$message = 'Hello World';

print <<EOF;
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>CGI Example</title>
<p>The date is <b>$date</b> and my message is <i>$message</i>.</p>

<%
myMessage = "Hello World"
myDate = date
myTime = time
%>

<p><%=message%></p>
<p><%=myDate%></p>
<p><%=myTime%></p>

ASP is the easiest language on the planet. After all, it's BASIC :)
 
P

Paul Furman

m said:
Toby A Inkster wrote:




Right. In general, Perl is easier to use than PHP after a short initial
learning period. It blows away C++ and Java for any kind of
administrative task. I think beginners panic seeing succinct markup like
'$_' and '@_' ; but it saves so much irritating typing,
and is actually not difficult to learn.

And, after all the work w3c has done getting display and content
seperated, why would anyone want to re-mud the waters by adding
a bunch of coding into the middle of an (X)HTML page?

PHP has a tremendous amount of pre-built functions that come out of the
box for web related things like cookies and sessions. It's my
understanding that Perl can be a lot of work to do things that are
one-step in PHP. I'm pretty new to it though & don't know anything about
Perl. I think PHP is considered a sloppy language that is not as precise
but I appreciate that as someone who is not primarily (or hardly) a
programmer.
 
T

Toby A Inkster

SpaceGirl said:
<%
myMessage = "Hello World"
myDate = date
myTime = time
%>

<p><%=message%></p>
<p><%=myDate%></p>
<p><%=myTime%></p>

ASP is the easiest language on the planet. After all, it's BASIC :)

It's almost exactly the same in PHP:

<?
$myMessage = "Hello World";
$myDate = date("d-m-y");
$myTime = date("H:i:s");
?>

<p><?= $message ?></p>
<p><?= $myDate ?></p>
<p><?= $myTime ?></p>

However, ASP gets complicated very quickly. For example, today I needed to
write an ASP (VB)script to list the files in a directory ("/uploads") on
the webserver, not knowing the absolute path to the directory.

I had to use this code:

<%
Dim objFSO, objFolder, objFile, f
Response.ContentType = "text/plain"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
f = Server.MapPath("/uploads")
Set objFolder = objFSO.GetFolder(f)
For Each objFile in objFolder.Files
Response.Write objFile.Name & Chr(13) & Chr(10)
Next
%>

Whereas in Perl I could have just done:

#!/usr/bin/perl
print "Content-Type: text/plain\n\n";
system("ls -1 uploads");

Easy peasy.

PHP would be just as easy:

<?
header('Content-Type: text/plain');
print `ls -1 uploads`;
?>

Perl makes easy things easy and hard things possible. VBscript tends to
make easy things easy and moderately complicated things unthinkable.
 
M

m

Paul said:
PHP has a tremendous amount of pre-built functions that come out of the
box for web related things like cookies and sessions. It's my
understanding that Perl can be a lot of work to do things that are
one-step in PHP.

use CGI ':standard';
# Above single line turns Perl into a web language.
# The CGI module has everything you need to do web stuff
# easily. But, no, you wouldn't want to write it from
# scratch instead of including the module! Other modules
# let you turn Perl into web robots, mail clients, and such --
# all in the same program if you want!.

print "Set-Cookie: zot=$zip;expires=30-Feb-2004 00:00:00 GMT\n";
# ...and the above line sets a cookie with an expiration date.
# That's all there is to it. Reading them is just as easy.
 
J

Jeffrey Silverman

Any way to get this to work with just pure HTML? I found that I can include
a html file into another one, but then I'd have to change it to .shtml,
which doesn't work well here for some reason.


While pure HTML won't work, you *can* configure the web server (Apache,
say) to process .html files for SSI.

Example (this goes inside httpd.conf):

#
# To use server-parsed HTML files
#
AddType text/html .shtml .html .booger
AddHandler server-parsed .shtml .html .booger

# There, I just configured Apache to process all *.booger files as
# SSI files, as well as .html *and* .shtml

Note: Parsing *every* .html file will, however, add overhead and will slow
down the server a smidge. But not much.
 
D

David Mackenzie

<%
myMessage = "Hello World"
myDate = date
myTime = time
%>

<p><%=message%></p>
<p><%=myDate%></p>
<p><%=myTime%></p>

I understand that handling file uploads requires a third-party
component in ASP. PHP makes handing file uploads trivial.

For sending e-mail, just use PHP's mail() function. That literally
could not be easier!

ASP is okay, but PHP is better ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top