Esta classe é indicada para aqueles programadores ou webdesigners que pretendem criar um site simples, com conteúdo estático, de preferência, mas que querem ter a facilidade e praticidade para fazer manutenções depois, já que a programação vai ficar separada do template.
Nada impede, contudo, que você crie sistemas complexos com a clase, mas seu objetivo primordial é ser usada em sites pequenos e sem grande complexidade.
O funcionamento segue uma ideia bastante simples: fazer uma leitura de um arquivo de templates dentro de uma string, uma pesquisa e repor todas as ocorrências com um ou mais argumentos.
É composta por 2 arquivos, tpl.class.php e example.php
tpl.class.php
<?php
/***************************************************************************
*
* Author : Eric Sizemore ( www.secondversion.com & www.phpsociety.com)
* Package : Simple Template Engine
* Version : 1.0.2
* Copyright: (C) 2006 - 2007 Eric Sizemore
* Site : www.secondversion.com
* Email : esizemore05 @ gmail.com
* File : tpl.class.php
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
***************************************************************************/
// Template engine
class template
{
/**
* Template variables and their replacements
*
* @var array
*/
var $tpl_vars;
/**
* Constructor
*/
function template()
{
$this->tpl_vars = array();
}
/**
* Assign our variables and replacements
*
* @param array Template variables and replacements
* @return none
*/
function assign($var_array)
{
// Must be an array...
if (!is_array($var_array))
{
die('template::assign() - $var_array must be an array.');
}
$this->tpl_vars = array_merge($this->tpl_vars, $var_array);
}
/**
* Parse the template file
*
* @param string Template file
* @return string Parsed template data
*/
function parse($tpl_file)
{
// Make sure it's a valid file, and it exists
if (!is_file($tpl_file))
{
die('template::parse() - "' . $tpl_file . '" does not exist or is not a file.');
}
$tpl_content = file_get_contents($tpl_file);
foreach ($this->tpl_vars AS $var => $content)
{
$tpl_content = str_replace('{' . $var . '}', $content, $tpl_content);
}
return $tpl_content;
}
/**
* Output the template
*
* @param string Template file
*/
function display($tpl_file)
{
echo $this->parse($tpl_file);
}
}
?>
example.php
<?php
/***************************************************************************
*
* Author : Eric Sizemore ( www.secondversion.com & www.phpsociety.com)
* Package : Simple Template Engine
* Version : 1.0.2
* Copyright: (C) 2006 - 2007 Eric Sizemore
* Site : www.secondversion.com
* Email : esizemore05@gmail.com
* File : example.php
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
***************************************************************************/
// Instantiate class
require_once('tpl.class.php');
$tpl =& new template();
/**
* assign expects an array of:
* variable => value
*
* Variables in your template(s) should be in the form of:
* {variable}
*/
$tpl->assign(array(
'title' => 'Simple Template Engine Test',
'content' => 'This is a test of the <a href=" http://www.phpclasses.org/browse/package/3171.html">Simple Template Engine</a> class by Eric Sizemore.'
));
// Parse the template file
$tpl->display('example.tpl');
?>
O arquivo TPL, que é o template a ser usado
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
" http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>{content}</p>
</body>
</html>
Espero que ajude a todos, e qualquer dúvida, podem comentar…
Me siga no Twitter para ficar por dentro das novidades do blog.
Leia Também
Tags: arquivo tpl, PHP, php tpl, Scripts PHP, template php, template tpl










