PhpSnip.com

User Stats

Simplest template

Template function/class slows down site a lot, especially if it is implemented in PHP. This is an example code how output bufferring and include() can be used as template.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 256

Source Code ( 55 lines )

<?php
// FILE: simple_template.php
// This file uses temple.tpl
//
// If you need to deal with array and do not want to use
// array syntax in template, then use extract().
// http://www.php.net/manual/en/function.extract.php
// With extract(), you should need minimum PHP code in
// template files when you have loop in template.
// (If PHP supports macro, it would be almost the same as
//  other template systems. Macro is not supported
//  currently.)
//
// Once you understand how output buffer and include could 
// be used, you should be able to separate code and html
// just like other template systems.
//
// I don't use any template function/class. Let me 
// know if you think you need template function/class.
//
// yohgaki@hotmail.com
//
/*
 Note: To include template.tpl correctly, you need
       to enable ASP style tag in php.ini.
       I use <%= %> syntax, since <?php= ?> is not
       supported. (<?= ?> syntax has problem with XML)
       <%=$var%> is equivalent to <% echo $var %>
*/

$title = 'Template Test';
$msg1 = 'Test message 1';
$msg2 = 'Test message 2';

ob_start();
include('template.tpl');
$html = ob_get_contents();
ob_end_clean();

print $html;
?>


// FILE: template.tpl
// Save following file as template.tpl
// This file is used by simple_template.php
<head>
<title><%=$title%></title>
</head>

<body>
$msg1 = <%=$msg1%><br>
$msg2 = <%=$msg2%><br>
</body>
</html>

Search

Subscribe

  Rss Feeds

Sponsors

Advertise