Simple PHP templates

jstfsklh211 0 Tallied Votes 429 Views Share

A simple way for using templates in php new pages can run server side code or pretty much do anything you would normally want to do on a web page

First create your template.
cleary there is way more that can be done with this, I've kept it simple for demonstation purposes.

<?php
//page variables for output
//@@@@@@@@@@@@@@@@@@@@@
//  $metaInclude
//  $titleInclude
//  $headInclude
//  $bodyInclude
//@@@@@@@@@@@@@@@@@@@@@
?>
<!DOCTYPE html>
<html lang="en"><head>
    <?php if (isset($metaInclude)) { echo $metaInclude; }  else { ?>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="keywords" content=""/>
        <meta name="description" content=""/>
    <?php } ?>
    <?php if (isset($titleInclude)) { echo $titleInclude; }  else { ?>
        <title>Simple PHP Template</title>
    <?php } ?>
    <?php if (isset($headInclude)) { echo $headInclude; } ?>
</head><body>
    <?php if (isset($bodyInclude)) { echo $bodyInclude; } ?>
</body></html>

Once your Template is completed you can begin to create new pages

<?php ob_start(); ?>
    Hello World
<?php
    $titleInclude = ob_get_contents();
    ob_end_clean();
?>
<?php ob_start(); ?>
    <h1>Hello World</h1>
<?php
    $bodyInclude = ob_get_contents();
    ob_end_clean();
    require_once('template.php');
?>

A good old fashion "hello world" page
Create only the variables you want to use then simply include the template page and your done

<?php
//page variables for output
//@@@@@@@@@@@@@@@@@@@@@
//  $metaInclude
//  $titleInclude
//  $headInclude
//  $bodyInclude
//@@@@@@@@@@@@@@@@@@@@@
?>
<!DOCTYPE html>
<html lang="en"><head>
	<?php if (isset($metaInclude)) { echo $metaInclude; }  else { ?>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta name="keywords" content=""/>
		<meta name="description" content=""/>
	<?php } ?>
	<?php if (isset($titleInclude)) { echo $titleInclude; }  else { ?>
		<title>Simple PHP Template</title>
	<?php } ?>
	<?php if (isset($headInclude)) { echo $headInclude; } ?>
</head><body>
	<?php if (isset($bodyInclude)) { echo $bodyInclude; } ?>
</body></html>
blocblue 238 Posting Pro in Training Featured Poster

Quick piece of feedback...

In your example, either the $titleInclude variable needs to include the <title> tags, or you need to wrap the output in the tags within the template.

Also, I think the use of the output buffer for capturing a single page title value is a bit much. Was there a reason for using this, rather than just assigning a string to the variable directly?

gadgetandgear 0 Newbie Poster

Thank you very much for the info :)

Member Avatar for diafol
diafol

I agree with blocblue the OB is a bit of an overkill.

jstfsklh211 79 Light Poster

granted for title the OB is overkill I usually dont have title set up like that hense the missing tags

usually I just have a string assigned to title inside the tags

<title>
    <?php print (isset($titleInclude) ? $titleInclude : 'Simple PHP Template'); ?>
</title>

something like that

I just wasnt careful enough when I was simplifying the code. The OB is great for anything more than a simple string.

jstfsklh211 79 Light Poster

Quick add in "sub-templates" might not be the right term but

you can easily create sub-templates to use for similar pages and i add this because i hate not being able to easily add js/css to the head from inner pages its so frustrating dealing with legacy code using a few includes

//sub_template.php
<?php ob_start(); ?>
<div>
    some html blah blah blah
    <?php if (isset($subInclude)) { echo $subInclude; } ?>
</div>
<?php
$bodyInclude = ob_get_contents();
ob_end_clean();
require_once('template.php');
?>


//new_page.php
<?php ob_start(); ?>
    <script src="myjs.js"></script>
<?php
$headInclude = ob_get_contents();
ob_end_clean();
require_once('template.php');
?>
<?php ob_start(); ?>
    some html blah blah blah
<?php
$subInclude = ob_get_contents();
ob_end_clean();
require_once('sub_template.php');
?>

notice that you can still access the head tag using the sub-template

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.