Hi,
Can anybody provide me some examples of using templates in php
Brief practical guide to PHP templating (building on ’s question and the replies from , and ). There are three pragmatic approaches: 1) use plain PHP files as views and include them from controllers (simple and fast), 2) use a tiny placeholder renderer when you want stricter separation, or 3) adopt a mature template engine / framework view layer when the project grows. Trade-offs: plain PHP is easiest; engines give auto‑escaping, inheritance, and caching.
A tiny renderer pattern is useful when you want separation without a full dependency. Keep templates as static markup with named placeholders and pass a short, explicit array of values. Example (safe-ish, minimal):
function renderTemplate($path, array $data = []) {
$tpl = file_get_contents($path);
foreach ($data as $k => $v) {
$tpl = str_replace('{{'.$k.'}}', htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'), $tpl);
}
return $tpl;
} When to pick what: for one-off sites or prototypes plain PHP views are fine; if multiple designers or strict sandboxing are required, pick an engine (they add learning overhead but help prevent XSS and enable caching). If you’re using a framework, use its built-in view layer rather than inventing a custom system.
Practical rules and gotchas: keep templates free of DB queries or heavy logic; pass only necessary, sanitized data; never include files based on raw user input; enable template caching in production; avoid extract() unless you control the data (it hides variable origins); check error logs and file paths (use absolute paths or DIR); and add a simple dev-mode that shows template errors. These steps keep presentation tidy, secure, and maintainable as the app grows.
Jump to Post— humbug 5There are functions for including in a script, a script held in another file. Is this what you mean? i.e. you can define the functions that you will use (and constants, variables, etc) and then
Jump to Post— almostbob 866a template
<html>
<head>
<style>
.left {width:50% text-align:right; float:left;}
.right{width:50% text-align:left; float:right;}
</style>
</head>
</body>
<div class=left><?php include('left.php'); ?></div>
<div class=right><?php include('right.php'); ?></div>
</body>
</html>a trivial example, yet without any idea of
'my requirement' its difficult
the template above is the <style></style> in the head
typically the …
PHP templates? what do you mean? like frameworks?
PHP templates? what do you mean? like frameworks?
Yes, if I want to create one according to my requirement.
There are functions for including in a script, a script held in another file. Is this what you mean? i.e. you can define the functions that you will use (and constants, variables, etc) and then include them.
Syntax:
include 'functions.php';
include "variables.inc"; See also, include_once, require and require_once.
No,
I actually mean to seperate presentation layer from business logic.
a template
<html>
<head>
<style>
.left {width:50% text-align:right; float:left;}
.right{width:50% text-align:left; float:right;}
</style>
</head>
</body>
<div class=left><?php include('left.php'); ?></div>
<div class=right><?php include('right.php'); ?></div>
</body>
</html>
a trivial example, yet without any idea of
'my requirement' its difficult
the template above is the <style></style> in the head
typically the layout is created in an external stylesheet, that specifies,
absolute (bad only works on 1 screensize) or
relative (good)
placement and size colors transparency visibility of the elements to appear on a html page.
then the stylesheet is referenced in each pages source and ensures uniformity of appearance.
also allows for easy alteration of appearance by editing a single stylesheet
in php it is simple,
each page is (trivial example)
<?php ob_start("ob_gzhandler");
include ('header file');
/* some code to generate a body or process data */
?>
<LINK Rel="stylesheet" Type="text/css" Media="screen" href="/style.css.php">
</head>
<body>
some static html
<?php
/* some more php to do something else */
include ('footer file');
ob_flush();
?> where header file includes the lines
<LINK Rel="stylesheet" Type="text/css" Media="screen" href="/style.css.php">
<script type=text/javascript language='javascript' src='/script.js.php'> edit: in my sites all files are given php extensions and the first lines of code are
<?php
header ('content-type: /* [b]actual content:: text html text/css text/javascript[/b] */');
ob_start("ob_gzhandler"); ?> the last line of code is
<php
ob_flush();
?> in case mod_deflate is not enabled in the server
A CSS template
style.css.php
<?php header ('content-type: text/css');
ob_start("ob_gzhandler"); ?>
/*<style>*/
.rss-box { margin: 1em 3%; padding: 4px 8px; background-color: #ededed; border: 2px dashed #7485CA; }
.rss-title, rss-title a { font-family: "American Typewriter", "Trebuchet MS", Trebuchet, Lucida, sans-serif; font-size: 1em; font-weight:bold; margin: 5px 0; padding: 0; letter-spacing: 1px; }
.rss-items { }
.rss-item { font-family: verdana, arial, sans-serif; font-size: .95em; font-weight : bold; margin: 8px 0; }
.rss-item a:link, .rss-item a:visited, .rss-item a:active { text-decoration : none; border-bottom: 1px solid #edefed; color: #88b; }
.rss-item a:hover { text-decoration : none; color: #e0861e; border-bottom: 1px dotted #e0861e; }
.rss-date { font-size: 0.90em; font-weight : normal; color: #F60; }
/*test layout area, lt left block rt right block b* containers*/
.lt { float:left; text-align:right; width:30%; }
.rt { float:right; text-align:left; width:65%; }
.bk { width:90%; border: #778899 solid 0px; margin: 2px 5%; padding:2px; }
.bkb { width:90%; border: #778899 solid 1px; margin: 2px 5%; padding:2px; }
.block { float:left; border: #778899 solid 1px; margin:5px; padding:5px; }
.blockr { float:right; text-align:left; border: #778899 solid 1px; margin:5px; padding:5px; }
/*end test area*/
@media all { .dontall {display:none; } }
@media print { .dontprint { display:none; } }
@media screen { .dontshow { display:none; } .doshow { display:inline; } }
a { font-size: 90%; text-decoration: none; }
a:hover { background-color: #66cdaa; text-decoration: none; }
a:focus { background-color: #66cdaa; text-decoration: none; }
a:active { background-color: #66cdaa; text-decoration: none; }
a.button { font-size: 80%; background-color: #d4d0c8; color: #000000; cursor: pointer; overflow: visible; padding: 2px; }
a.button:hover { background-color: #66cdaa; }
a.button:focus { background-color: #66cdaa; border: 1px solid #000000; }
a.button:active { background-color: #66cdaa; border: 1px solid #000000; }
body { background-color: #f6f6f6; color: #000000; font-family: verdana, geneva, arial, serif; font-size: 100%; margin: 15px; margin-top:0; padding-left: 0px; padding-right: 0px; padding-top: 0px; padding-bottom: 1px; }
button { background-color: #d4d0c8; font-size: 80%; padding: 2px; border:1px; cursor: pointer; margin: 1px; overflow: visible; }
button:hover { background-color: #66cdaa; }
button:active { background-color: #66cdaa; border-left: 3px; border-bottom: 3px; text-enhance: bold; }
button:focus { background-color: #66cdaa; border-left: 3px; border-bottom: 3px; text-enhance: bold; }
div { text-size:100%; }
form { PADDING: 1px; margin: 1px; }
img { border: 0px; }
input { background-color: #efefef; border: 0px; border-bottom: 1px solid #000000;font-size: 90%; }
input:hover { background-color: #dfdfdf; }
input:focus { background-color: #dfdfdf; border-bottom: 2px solid #000000; }
input:active { background-color: #dfdfdf; border-bottom: 2px solid #000000;}
input[type=button] { background-color: #d4d0c8; border: 1px solid; font-size: 90%; }
input[type=button]:hover { background-color: #66cdaa; }
input[type=button]:focus { background-color: #66cdaa; border: 2px solid #000000; }
input[type=button]:active { background-color: #66cdaa; border: 2px solid #000000; }
p { padding: 1px; font-size: 100%; list-style-type: square; }
p:first-line { font-weight:bold; }
p:first-letter { font-size:200%; float:left; }
table { padding: 1px; }
table.sortable th { background-color: #dedede; }
table.sortable tr.odd td { background-color: #ededed; }
table.sortable tr.even td { background-color: #dedede; }
table.sortable tr.sortbottom td { background-color: #dfdfdf; font-weight: bold; }
.top { background-color: #000070; color: #ffffff; text-align: right; }
td.top { background-color: #000070; color: #ffffff; text-align: right; }
td.bottom { background-color: #efefef; padding: 15px; }
.bottom { background-color: #efefef; padding: 15px; }
textarea { background-color: #efefef; color: #460606; font-size: 90%; font-weight: bold; }
textarea:focus { background-color: #dfdfdf; color: #460606; font-size: 90%; font-weight: bold; }
textarea:active { background-color: #dfdfdf; color: #460606; font-size: 90%; font-weight: bold; }
ul li { PADDING: 1px; }
.infor { color: #329932; font-size: 90%; font-weight: bold; background-color: #efefef; border: 0px; }
.logo { background: transparent; color: #000000; text-align: right; font-size: 85%; top: auto; left: auto; bottom: 1px; right: 1px; position: fixed; }
.logomid { background-color: #eeeeee; color: #000000; text-align: right; font-size: 85%; top: auto; left: auto; position: fixed; bottom: 300px; right: 3px; }
.log1 { background: transparent; color: #000000; text-align: left; font-size: 85%; top: auto; left: 1px; position: fixed; bottom:1px; }
.log2 { background: transparent; color: #000000; text-align: left; font-size: 85%; top: auto; bottom: 1px; left: 5px; right: auto; position: fixed; }
.logtop { color: #000000; background: transparent; margin: 0; background-position: center center; text-align: right; font-size: 85%; top: 200px; left: 1px; right: auto; position: fixed; }
.logotop { color: #000000; background: transparent; text-align: right; font-size: 85%; top: 200px; left: auto; bottom: auto; right: 1px; position: fixed; }
.menu { font-size: 90%; font-weight: bold; color: #000000; background-color: #d4d0c8; cursor: pointer; }
.menuitems{ padding-left:10px; padding-right:10px; }
.mouseovermenu { font-size: 90%; font-weight: bold; color: #000000; background-color: #f6f6f6; cursor: pointer; }
.mouseover { font-size: 90%; font-weight: bold; color: #000000; background-color: #f6f6f6; }
.required { color: #662300; background-color: #f6f6f6; font-size: 100%; font-weight: bold; border: 0px; }
.smalldisplay { background-color: #f6f6f6; text-align: right; font-weight: bold; color: #000000; font-size: 85%; }
.smalltext { background-color: #f6f6f6; color: #000000; font-size: 85%; }
.text { font-size: 100%; }
.tinydisplay { background-color: #f6f6f6; text-align: right; font-weight: bold; color: #000000; font-size: 77%; }
.tinytext { background-color: #f6f6f6; color: #000000; font-size: 75%; }
.topmenu { background-color: #f6f6f6; text-align: right; top: 1px; left: auto; bottom: auto; right: 1px; position: fixed; }
.skin0{ position:absolute; width:165px; border:2px solid #778899; background: #eeeeee; font-family:Verdana; line-height:20px; cursor:default; font-size:14px; z-index:100; visibility:hidden; }
.menuitems{ padding-left:10px; padding-right:10px; }
<?php ob_flush(); ?> he isn't asking for a css or any html/xhtml template...
the delhi guy was asking for something like the Zend, CakePHP, PRADO, seagull, and etc. framework.
he isn't asking for a css or any html/xhtml template...
the delhi guy was asking for something like the Zend, CakePHP, PRADO, seagull, and etc. framework.
Yes, you are right.
you know when you use a template like them, they have a manual or documentation provided at there website.... on there manual, examples are provided.. so its going to cost some reading and testing.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.