Making duplicate code generic

This is a discussion on "Making duplicate code generic" within the Web Page Design section. This forum, and the thread "Making duplicate code generic are both part of the Design Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jul 14th, 2006, 06:39
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Making duplicate code generic

Hi,

I have the following code in 3 diffreent HTML pages:

<select name="select1" size="1" style="background-color:#FFFFD7">
<option value="./a.htm" target="mainFrame">A</option>
<option value="./b.htm" target="mainFrame">B</option>

What im wondering is that is it possible to modularise this in some way like the way javascript can be added to a .js file. Save me duplicating this code 3 times.

I just want a central place for all duplicated code in forms to save me having to change each occurance.

I been told to use Snippets, but doesnt this just make it easier to copy and paste in between pages?

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jul 14th, 2006, 10:02
minute44's Avatar
Most Reputable Member

SuperMember
Join Date: Apr 2006
Location: Nottingham UK
Age: 25
Posts: 1,351
Blog Entries: 1
Thanks: 0
Thanked 1 Time in 1 Post
Re: Making duplicate code generic

Quote:
Originally Posted by Rudie View Post
Hi,

I have the following code in 3 diffreent HTML pages:

<select name="select1" size="1" style="background-color:#FFFFD7">
<option value="./a.htm" target="mainFrame">A</option>
<option value="./b.htm" target="mainFrame">B</option>

What im wondering is that is it possible to modularise this in some way like the way javascript can be added to a .js file. Save me duplicating this code 3 times.

I just want a central place for all duplicated code in forms to save me having to change each occurance.

I been told to use Snippets, but doesnt this just make it easier to copy and paste in between pages?

Thanks.
If you're using Dreamweaver or some other WYSIWYG editor then chances are you'll be able to use the library function. this allows you to save imagesm text and code snippets as library items so you can insert them whenever. Kind of like an advanced clipboard.

Hope this helps...
Last Blog Entry: Annoying people.... (Jan 16th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jul 14th, 2006, 10:48
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Yes, Im using Dreamweaver 8. I'll give that a try when i get home.
I know I can re-use javascript code from the .js files but because this is just HTML, didnt want to code the same piece of code 10 times.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jul 14th, 2006, 10:51
minute44's Avatar
Most Reputable Member

SuperMember
Join Date: Apr 2006
Location: Nottingham UK
Age: 25
Posts: 1,351
Blog Entries: 1
Thanks: 0
Thanked 1 Time in 1 Post
Re: Making duplicate code generic

Quote:
Originally Posted by Rudie View Post
Yes, Im using Dreamweaver 8. I'll give that a try when i get home.
I know I can re-use javascript code from the .js files but because this is just HTML, didnt want to code the same piece of code 10 times.

Thanks.
yep, you'll definately be able to do it... you can save anything as a library item in DW. I've done a huge intranet for the place where I work and library items have saved my skin more than once on that project.
Last Blog Entry: Annoying people.... (Jan 16th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jul 14th, 2006, 16:21
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Hi,

Ive highlighted the code I want to make generic but I cant get the button to add the code into the library enabled. Even the option Modify, Library, Add Object To Library is disabled...

How can I enable?

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jul 14th, 2006, 16:32
Rob's Avatar
Rob Rob is offline
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,192
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
Re: Making duplicate code generic

Maybe you should consider SSI

This can be done in ASP, PHP, JSP, and any other dynamic scripting language.

It can also be achieved using the .shtml extension should you host support SSI.
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jul 14th, 2006, 16:33
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Ok, so cant I use this using normal HTML?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jul 14th, 2006, 16:36
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Sorted, I had to add a Site.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jul 14th, 2006, 19:15
Highly Reputable Member
Join Date: Aug 2005
Location: 3rd Branch Up, Old Oak.
Age: 48
Posts: 658
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

I'm just starting to experiment with CSS and I'm just wondering if you could add the code to an external style sheet and just link any pages to it which require this code?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Jul 14th, 2006, 19:20
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Not sure, but I dont like the idea of libraries as its still duplicating code even though a change to the library will update the code used elsewhere.

Surely theres a way to avoid this?

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Jul 14th, 2006, 19:22
Most Reputable Member
Join Date: Aug 2005
Location: North Wales, United Kingdom
Age: 21
Posts: 1,093
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Your on the wrong wave length than the comments here Rudie.

Your talking about how to do it in Dreamweaver, whereas the rest of the comments have been about hard coding.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Jul 14th, 2006, 19:27
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Sorry, Eastenders is putting me off ;-)

I basically want to put common code into a seperate place or file and then call it from the web pages where i want to place it, mainly a drop down list.

Hope I make sense.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Jul 14th, 2006, 20:01
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

I think the point has been missed a few times here.

You put the generic code into a file, e.g., 'generic.inc' which you load on the server alomg with your other html files.

Now, as Rob indicated previously, you use one of the scripting methods to 'include' this file at the appropriate place(s) in your html files.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Jul 14th, 2006, 20:05
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Oh ok, is there an example call somewhere?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Jul 14th, 2006, 21:35
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

PHP example.

...
...
html code
...

<?php
include ('generic.inc');
?>


...
more html code
...

As Rob stated, if you don;t have access to php but you can use SSI (Server Side Includes), then name your file 'generic.shtml' and use:

<!--#include file='generic.shtml'-->
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Jul 14th, 2006, 22:11
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Sorry for sounding thick, but if the generic.shtml (i dont know PHP thats why ive chose SSI) includes my drop down list code, how do I bring in the code from the generic.shtml onto my main page?

Is there a way to wrap it into a function or something?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #17  
Old Jul 14th, 2006, 22:56
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

You just position the include statement at the point in your html where you want the generic code included.

The server effectively merges the two files together before sending it to the browser.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #18  
Old Jul 15th, 2006, 09:55
Junior Member
Join Date: Jun 2006
Location: England
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Hi,

Im almost there, ive saved the common code into a .shtml file, called this from within another page, the cut out code shows up in design view, but on execution, it does not show. Can anyone please help?

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #19  
Old Jul 15th, 2006, 10:14
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Making duplicate code generic

Let's see both your generic code and the section of code from your main pages(s) that include the generic code.

Include the code either side of where you include the generic file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #20  
Old Jul 15th, 2006, 10:20