HelmBurger
HelmBurger

Reputation: 1298

how to modularize html page elements using php

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

Answers (3)

Zlatin Zlatev
Zlatin Zlatev

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:

  1. Smarty http://smarty.incutio.com/?page=SmartyFrequentlyAskedQuestions
  2. Mustache PHP https://github.com/bobthecow/mustache.php
  3. PHP TAL http://phptal.org/

Of course if you just want some simple header and footer - use include_once as suggested by the other answers.

Upvotes: 0

Manoj
Manoj

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

user3615979
user3615979

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

Related Questions