Jump to content

[PHP] Sistema de templates


Recommended Posts

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>


Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum!

Link to comment
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

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.