Reputation: 1298
I want to modularize html page elements using PHP. This is the code I am currently using:
<?php include("template.php");
echo "Hello";?>
The template.php actually also includes the body of original html page, inside of which I wish to write "Hello". So will I have to create 3 different PHP templates: 1 for header, 1 for navigation, and 1 for footer and then include them at appropriate locations in a new PHP file or is there any other way out?
Upvotes: 1
Views: 935
Reputation: 3098
How about using a template engine? If you want to have same complex layout for multiple pages with some variations on the datasources a template engine would be of real help. Some good template engines for php are:
Of course if you just want some simple header and footer - use include_once as suggested by the other answers.
Upvotes: 0
Reputation: 1
I suggest you use the include_once()
function to include the scripts you want to insert and place the code into html <div></div>
tags so you can display wherever you need them to display in the web page.
<div>
<?php include_once("header.php")?>
</div>
<div>
<?php include_once("body.php")?>
</div>
<div>
<?php include_once("footer.php")?>
</div>
Upvotes: -1
Reputation: 47
You could include the 3 files on each page?
<?php include("header.php"); ?>
<?php include("navigation.php"); ?>
Hello
<?php include("footer.php"); ?>
You could also include the navigation.php inside the header.php
Upvotes: 2