24
Jan
Postado por Chavão em Programação
Olá pessoal, faz tempo que não escrevo nesse blog! Hoje a tarde pensei em postar uma mensagem de despedida e encerramento, mas como eu havia dormido tarde na noite anterior e acordado cedo eu acabei pegando no sono a tarde e estou aqui agora de madrugada acordadão e sem sono.
Então aproveitando esse momento silencioso aqui em casa eu resolvi brincar um pouco com PHP, comecei a fazer um template manager, porque o que mais me deixa enojado do meu código é quando tenho que misturar HTML com PHP, aí fica aquela zona.
Então vamos ao código… Esse é um código de exemplo de como fica a parte PHP usando o meu Template Manager:
-
<?php
-
-
include ‘CTemplate.php’;
-
-
$objT = new CTemplate(‘new.html’,‘templates’);
-
-
$linha[0] = "Zero";
-
$linha[1] = "One";
-
$linha[2] = "Two";
-
$linha[3] = "Three";
-
-
$teste[0] = "1";
-
$teste[1] = "2";
-
$teste[2] = "3";
-
$teste[3] = "4";
-
$teste[4] = "5";
-
-
$objT->assign(‘title’,‘Test page’);
-
$objT->assign(‘variavel_teste’,"New value to be placed at variavel_teste");
-
$objT->assign(‘linha’,$linha);
-
$objT->assign(‘teste’,$teste);
-
$objT->display();
-
-
?>
Agora o HTML para ser manipulado:
-
<html>
-
<head>
-
<title>{title}</title>
-
</head>
-
<body>
-
{variavel_teste}
-
-
<br/>
-
<br/>
-
-
== {linha} ==<br/>
-
-
<ul>
-
<li>{teste}</li>
-
</ul>
-
-
<br/>
-
-
<ol>
-
<li>{teste}</li>
-
</ol>
-
</body>
-
</html>
E o HTML resultante da renderização com o Template Manager:
-
<html>
-
<head>
-
<title>Test page</title>
-
</head>
-
<body>
-
New value to be placed at variavel_teste
-
-
<br/>
-
<br/>
-
-
== Zero ==<br/>
-
== One ==<br/>
-
== Two ==<br/>
-
== Three ==<br/>
-
-
<ul>
-
<li>1</li>
-
<li>2</li>
-
<li>3</li>
-
<li>4</li>
-
<li>5</li>
-
</ul>
-
-
<br/>
-
-
<ol>
-
<li>1</li>
-
<li>2</li>
-
<li>3</li>
-
<li>4</li>
-
<li>5</li>
-
</ol>
-
</body>
-
</html>
Existem duas formas de iniciar o Objeto Template:
-
$objT = new CTemplate(‘new.html’,‘templates’);
ou
-
$objT = new CTemplate();
-
$objT->setPath(‘templates’);
-
$objT->setTemplate(‘new.html’);
A forma de codificar foi inspirada no Smarty Template, o primeiro Template Manager que eu usei. A classe do template manager foi batizado de “Chavão Template Manager” (que nome mais óbvio) e poder ser vista a seguir:
-
<?php
-
class CTemplate {
-
var $sHtml;
-
-
-
function __construct($psHtml=null,$psPath=null)
-
{
-
-
{
-
self::
$sPath =
(substr($psPath,
-1)==
"/") ?
$psPath :
$psPath.
"/";
-
}
-
-
-
{
-
-
-
{
-
include self::$sPath.$psHtml;
-
}
-
-
-
}
-
}
-
-
function setPath($psPath)
-
{
-
-
{
-
self::
$sPath =
(substr($psPath,
-1)==
"/") ?
$psPath :
$psPath.
"/";
-
}
-
}
-
-
function setTemplate($psHtml)
-
{
-
-
-
{
-
include self::$sPath.$psHtml;
-
}
-
-
-
}
-
-
function assign($pTarget,$psValue)
-
{
-
$this->arrTarget[$pTarget] = $psValue;
-
}
-
-
function display()
-
{
-
foreach($this->arrTarget as $sTarget => $sValue)
-
{
-
-
{
-
$this->
sHtml =
str_replace("{".
$sTarget.
"}",
$sValue,
$this->
sHtml);
-
unset($this->
arrTarget[$sTarget]);
-
}
-
}
-
-
$arrHtml =
explode("\n",
$this->
sHtml);
-
-
foreach($arrHtml as $iLine => $sHtml)
-
{
-
foreach($this->arrTarget as $sTarget => $arrValue)
-
{
-
-
{
-
foreach($arrValue as $sValue)
-
{
-
-
{
-
$sReturn .=
str_replace("{".
$sTarget.
"}",
$sValue,
$sHtml);
-
$arrHtml[$iLine] = $sReturn;
-
}
-
}
-
}
-
}
-
-
-
}
-
-
$this->
sHtml =
implode("",
$arrHtml);
-
-
-
}
-
}
-
-
?>
O “Chavão Template Manager” comentado e os arquivos de exemplo podem ser baixados clicando aqui
Gostou desse post? Compartilhe:
Conteúdos relacionados
One Response
Brijesh
Fevereiro 3rd, 2010 at 12:47 pm
1This template is nice. But i need more class files for learn.
RSS feed for comments on this post · TrackBack URI
Leave a reply