Jump to content

How to import repetitive code into a page?!

There must be a cleaner way of doing boxes

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to import repetitive code into a page?! There must be a cleaner way of doing boxes Rate Topic: -----

#1 User is offline   temhawk 

  • W.R. Private First-Class
  • Group: Members
  • Posts: 322
  • Joined: 30-August 07
  • Gender:Male
  • Interests:travel, cg art, macs, music, skateboarding, programming, discovery channel, TextMate 2

Posted 10 May 2008 - 02:53 PM

The reason I'm asking is because I often do "boxes" with graphical borders and corners in my pages.

For instance:
<div class="greenbox_TB"></div>
<div class="greenbox_LR"></div>
<div class="greenbox_TL"></div>
<div class="greenbox_TR"></div>
<div class="greenbox_BL"></div>
<div class="greenbox_BR"></div>

would create the sides and corners of a parent <div>, their css positions them absolutely with the appropriate graphics as background images.

It works fine, but I need a way to sum all of this up. There should be an invention for xhtml that allows you to sum up some code as something similar to class in css.

But that probably wont happen, so I'm asking for help on how to otherwise make my pages look more organized from a source-code point of view :D

I think I saw somewhere that .shtml files could import other files but I'm not sure if that's true and efficient.
Javascript can probably do this too, but I'd be interested in more options here!!

Help me out! ^_^
0

#2 User is offline   haku 

  • 日本語 Ninja
  • Group: Members
  • Posts: 652
  • Joined: 21-September 07
  • Gender:Male
  • Location:Yokohama, Japan

Posted 10 May 2008 - 09:04 PM

Javascript is not a good solution here, because your page will fall apart for anyone who turns it off.

I think you can use SSI (server side includes), although I've never used them, so I don't really know how well they work. They are probably your best bet with pure HTML pages though.

The other thing you can do (bit of a long route) is rename all your files with a .php extension, and use the php include() function to include them. For example, you save the 6 divs you mentioned above, and you save them in 'divs.php'. Then in your main document, at the place they should exist, you use this function:

include("divs.php")


This will bring those divs into your code. But you have a document with a .php extension instead of a .html(l) extension. So you use a mod rewrite in your .htaccess file so that whenever someone looks for ___.htm, it says that address in the address bar, but the page they see is ___.php. They don't know its php though. If you need help with that, I think I have the necessary code in a file here somewhere, I can help you with it.

The third option I can think of is to use your .htaccess file, and add some code into it so that it processes .html documents as php. So they look like an .html document to the user, but have the functionality of php, which means you can use the php include() function in it. I have this .htaccess code as well if you want it.

I think the first option is the best, though I can't offer much assistance on it.
The second option is probably the the next best, but the most difficult to implement.
The third option is probably the easiest (after SSI), but you will take a hit in the speed of your .html pages, because instead of being served to the browser as is, they need to be run though the php parser.
<a href="http://www.jaypan.com" target="_blank">Jaypan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
0

#3 User is offline   Steven Gardner 

  • W.R. Corporal
  • Group: Members
  • Posts: 160
  • Joined: 17-September 07
  • Gender:Male
  • Location:Fife, Scotland

Posted 12 May 2008 - 04:06 AM

View Posthaku, on May 11 2008, 02:04 AM, said:

Javascript is not a good solution here, because your page will fall apart for anyone who turns it off.

I think you can use SSI (server side includes), although I've never used them, so I don't really know how well they work. They are probably your best bet with pure HTML pages though.

The other thing you can do (bit of a long route) is rename all your files with a .php extension, and use the php include() function to include them. For example, you save the 6 divs you mentioned above, and you save them in 'divs.php'. Then in your main document, at the place they should exist, you use this function:

include("divs.php")


This will bring those divs into your code. But you have a document with a .php extension instead of a .html(l) extension. So you use a mod rewrite in your .htaccess file so that whenever someone looks for ___.htm, it says that address in the address bar, but the page they see is ___.php. They don't know its php though. If you need help with that, I think I have the necessary code in a file here somewhere, I can help you with it.

The third option I can think of is to use your .htaccess file, and add some code into it so that it processes .html documents as php. So they look like an .html document to the user, but have the functionality of php, which means you can use the php include() function in it. I have this .htaccess code as well if you want it.

I think the first option is the best, though I can't offer much assistance on it.
The second option is probably the the next best, but the most difficult to implement.
The third option is probably the easiest (after SSI), but you will take a hit in the speed of your .html pages, because instead of being served to the browser as is, they need to be run though the php parser.


Hey Haku, any chance you could send me the .htaccess code so i can convert an exsisting html site to process like a php site so i can use include's for this site without search engines throwing up 404 pages for html files that no longer exsisit if i convert pages from html to php.


Cheers

Steve G
0

#4 User is offline   marcamos 

  • W.R. General
  • Group: Administrators
  • Posts: 2,849
  • Joined: 04-April 06
  • Gender:Male
  • Location:Massachusetts - USA

Posted 12 May 2008 - 05:25 AM

Adding this to your root-level .htaccess file should do it:
AddType application/x-httpd-php .html


Otherwise, this search query might yield better results: http://www.google.com/search?&q=parse+...+with+.htaccess
0

#5 User is offline   haku 

  • 日本語 Ninja
  • Group: Members
  • Posts: 652
  • Joined: 21-September 07
  • Gender:Male
  • Location:Yokohama, Japan

Posted 12 May 2008 - 05:35 AM

That is one way of doing it. What the above code does is processes all .html forms as php. In the above case you can add php to your .html pages, without changing the page name. The only problem with it is that you will (apparently) take a hit on your .html pages that don't have any php in them, as they still have to be run through the php processor.

What I was speaking of is this code:

RewriteEngine on
RewriteBase /
RewriteRule ^([^.]+)\.html$ $1.php [L]


With this code, you rename all your pages with a .php extension. Then, when someone accesses _____.html, it shows them the contents of ____.php, but they still see _____.html in the address bar.

Both of these will work, you can choose whichever you want. Although Herkelees method may be a touch faster, as I believe my method involves a redirect (even though you don't see that redirect happening).
<a href="http://www.jaypan.com" target="_blank">Jaypan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users