• Revista PROGRAMAR: Já está disponível a edição #53 da revista programar. Faz já o download aqui!

Sign in to follow this  
Followers 0

[PHP]Sistema de templates

2 posts in this topic

Um pequeno sistema de templates que tinha por aqui.

class Template {
private $template;
private $buffer = array();

function __construct($template, $items){
	$this->template = $template;
	foreach($items as $k => $v) $this->buffer[(is_numeric($k) ? $v : $k)] = (is_numeric($k) ? '' : $v);

public function change($k, $v){
	$this->buffer[$k] = $v;
public function add($k, $v){
	$this->buffer[$k] .= $v;

public function generate(&$destiny = ''){
	foreach($this->buffer as $k => $v)
		$this->template = str_replace('<!-- '.$k.' -->', $v, $this->template);
	return ($destiny = $this->template);


include "lib/template_class.php";
$x = new Template(file_get_contents("index-blank.html"), array('head', 'head title', 'software name', 'software quick info', 'software navigation links'));

$x->change('head', '<script>alert("woot!");</script>');
$x->change('software name', 'lulzware');
$x->change('software quick info', 'oi?');
echo $x->generate();

Ficheiro index-blank.html:

   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <link rel="stylesheet" type="text/css" href="style.css" media="screen">
   <title><!-- head title --></title>
   <!-- head -->

<h1><!-- software name --></h1>
   <h2><!-- software quick info --></h2>

<ul id="navigation">
   <li><a href="#" title="About"><span>About</span></a></li>
   <!-- software navigation links -->

<div id="side-col">
   <ul id="services">
      <li style="background: url('images/web-design.png') no-repeat 10px 17px;"><a href="#" title="Some title">Header 1</a><br>
          Some description here</li>

   <!--<h3>New Header</h3>
      Some info here
   <p id="quote-link">
      <a href="#" title="Some title">Learn More...</a>

<div id="main-content">
   <div id="welcome">
      <h3>Operação realizada com sucesso!</h3>
          Some info

   <!-- main -->

   <p id="footer">
      Copyright © 2007 <a href="http://www.zymic.com/free-templates/">Free Templates</a> by <a href="http://www.zymic.com">Zymic</a> - <a href="http://www.zymic.com/free-web-hosting/">Free Web Hosting</a> - Best Viewed in <a href="http://www.mozillafirefox.us">Mozilla Firefox</a>



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0