473,808 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I avoid repeating code?

2 New Member
Greetings, everyone!

I'm new to web-development and have a lot of naive questions :) Right now I'm writing a simple site with very basic interface: the header, menu and content.

The content is the only thing that changes throughout the site. What I want to achieve is have all the HTML-code except content and head-tag in one place, so I don't have to edit each and every file when I need to update interface. So I want a single HTML-file for interface and a bunch of content-files.

So far I have only come up with writing a PHP-script that would read respective files and generate the output HTML-code. Of course, I would still have to make numerous of similar PHP-files but it doesn't really worry me because, unlike interface, I will no longer need to get back to it once it's done. But isn't it a serious compromise in terms of performance?..

I'm pretty sure there must be a standard solution but I can't seem to find an appropriate search request so I could google it.

Thanks in advance!
Sep 6 '10 #1
3 7044
kovik
1,044 Recognized Expert Top Contributor
Standard? Eh. The "standard," at least among newer programmers (and WordPress), is to store the header in a file (header.php) and the footer in a file (footer.php) and include them around the content in other files.

Personally, I like to create a template and insert data into the template. A simple way is to make a file that has a few variables it needs (such as $title and $content) and then the site files would define these properties.

Like so:


template.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title><?php echo $title; ?> - Site Name</title>
  4. </head>
  5. <body>
  6. <div id="header">
  7.   <h1><?php echo $title; ?></h1>
  8.   <ul id="nav">...</ul>
  9. </div>
  10. <div id="content">
  11.   <?php echo $content; ?>
  12. </div>
  13. <div id="footer">
  14.   Copyright &copy; 2010 Site Name
  15. </div>
  16. </body>
  17. </html>

index.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $title = 'Home';
  3. ob_start();
  4. ?>
  5. Welcome!
  6.  
  7. This is the content for the index.php file.
  8. We use output buffering (http://php.net/manual/en/ref.outcontrol.php)
  9. in order to store this data into a variable named "$content."
  10. <?php
  11. $content = ob_get_contents();
  12. ob_end_clean();
  13.  
  14. include 'template.php';
Sep 6 '10 #2
WoodyWoo
2 New Member
Thanks, worked perfectly for me!
Sep 7 '10 #3
kovik
1,044 Recognized Expert Top Contributor
It's what I do. ;)
Sep 7 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1633
by: Brian Lin | last post by:
Hi all, I have a table called Category and it has only two fields: CategoryID and CategoryName. I want user to be able to enter 10 categories at one page. So I what I do right now is to place 10 text input boxs on the page with name txt1, txt2, txt3, etc. After the user submit. I will check to see if each txt box is empty and insert into table if not. The problem is that I need duplicate the code for each text box. I am wondering if...
44
2600
by: shuisheng | last post by:
Dear All, Assume there are three classes where CA has members of class CA1 and CA2 as follows. To make the public functions of CA1 and CA2 can work on the members a1 and a2 in a CA object, I just write all the functions such as a1_func1(), a1_fun2(), a2_func1(), as_func2() in the CA interface. I think this is a stupid way since if there are more functions in CA1 and CA2, I need to repeat them all. Is there any good way to implement it?
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10631
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.