Pure PHP template function


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. function apply_template($file,$vars=array(),$include_globals=false){
  3. extract($vars);
  4. if($include_globals){
  5. extract($GLOBALS, EXTR_SKIP);
  6. }
  7. require($file);
  8. $template_html = ob_get_contents();
  9. header('Content-Length: ' . ob_get_length());
  10. return $template_html;
  11. }
  12. $template_vars['title'] = 'Hello World!';
  13. $template_vars['text'] = 'Hello World!';
  14. $template_vars['links'] = array(
  15. 'http://www.bigsmoke.us/php-templates/functions',
  16. 'http://www.php.net/'
  17. );
  18. $html = apply_template("_main.tpl.php",$template_vars);
  19. echo $html;
  20.  
  21. ///////////////////
  22. // _main.tpl.php //
  23. ///////////////////
  24. /*
  25. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  26. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  27. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr">
  28. <head>
  29. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  30. <title><?php echo $title; ?></title>
  31. <link rel="shortcut icon" type="image/png" href="favicon.png" />
  32. <link rel="stylesheet" href="./css/reset.css" type="text/css" media="all" />
  33. <link rel="stylesheet" href="./css/text.css" type="text/css" media="all" />
  34. <link rel="stylesheet" href="./css/style.css" type="text/css" media="screen, projection" />
  35. <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
  36.  
  37. <!--[if IE]>
  38. <link rel="stylesheet" href="./css/ie.css" type="text/css" media="screen, projection" />
  39. <![endif]-->
  40. <link rel="script" href="./scripts/javascript.js" type="text/javascript" />
  41. </head>
  42. <body>
  43. <p><?php echo $text; ?></p>
  44. <ul>
  45. <?php foreach($links as $link): ?>
  46. <li><a href="<?php echo $link; ?>" title="A Link"><?php echo $link; ?></li>
  47. <?php endforeach; ?>
  48. </ul>
  49. </body>
  50. </html>
  51. */
  52. ?>

URL: http://www.bigsmoke.us/php-templates/functions

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.