2019-05-30, 05:16 PM
(This post was last modified: 2019-05-30, 05:37 PM by DarkThinking.)
En esta guía te haré un simple administrador de templates.
Aclaro:
-> este script reemplaza un tag {dato} por una variable o el resultado de una función,
-> es simple y no necesita tanto conocimiento de php para adaptarlo.
Primero crearemos una funcion para obtener el archivo:
Luego creamos una funcion para "renderizar" el codigo
Ahora la funcion __construct y el set para poder poner lo reemplazable en el HTML
Ahora la funcion que REEMPLAZA los codigos tag
bueno esto funciona así:
en el HTML ponemos:
y se reemplazará por
$variableofuncion
Codigo completo:
Aclaro:
-> este script reemplaza un tag {dato} por una variable o el resultado de una función,
-> es simple y no necesita tanto conocimiento de php para adaptarlo.
Primero crearemos una funcion para obtener el archivo:
PHP Code:
<?php
class Template
{
private $tags = []; // tags
private $template; // archivo
public function getFile($file) // obtener archivo
{
if(file_exists($file)) // si existe el archivo
{
$file = file_get_contents($file); // obtiene los datos
return $file; // y retorna el archivo (los datos)
}
else
{
return false; // retorna 0 o falso en caso de error o no existir el archivo
}
}
Luego creamos una funcion para "renderizar" el codigo
PHP Code:
public function render() // renderizar o reemplazar tags
{
$this->replaceTags(); // reemplazar tags
echo $this->template; // imprimir los datos de php reemplazando los tags
}
Ahora la funcion __construct y el set para poder poner lo reemplazable en el HTML
PHP Code:
public function __construct($templateFile) // buscar archivo
{
$this->template = $this->getFile($templateFile);
if(!$this->template) {
return "Error! no puedo cargar el template -> $templateFile";
}
}
public function set($tag, $value) // crear tags
{
$this->tags[$tag] = $value;
}
Ahora la funcion que REEMPLAZA los codigos tag
PHP Code:
private function replaceTags() // reemplazar tags
{
foreach ($this->tags as $tag => $value) {
$this->template = str_replace('{'.$tag.'}', $value, $this->template);
}
return true;
}
}
bueno esto funciona así:
PHP Code:
<?php
require_once 'Template.php';
$tpl = new Template('archivo.html'); // creamos el template el cual se carga automaticamente
$tpl->set('codigoparahtml', $variableofuncion); // creamos el tag
$tpl->render(); // "renderizamos"
?>
en el HTML ponemos:
Code:
<h3>{codigoparahtml}</h3>
y se reemplazará por
$variableofuncion
Codigo completo:
PHP Code:
<?php
class Template
{
private $tags = []; // tags
private $template; // archivo
public function getFile($file) // obtener archivo
{
if(file_exists($file))
{
$file = file_get_contents($file);
return $file;
}
else
{
return false;
}
}
public function render() // renderizar o reemplazar tags
{
$this->replaceTags();
echo $this->template;
}
public function __construct($templateFile) // buscar archivo
{
$this->template = $this->getFile($templateFile);
if(!$this->template) {
return "Error! no puedo cargar el template -> $templateFile";
}
}
public function set($tag, $value) // crear tags
{
$this->tags[$tag] = $value;
}
private function replaceTags() // reemplazar tags
{
foreach ($this->tags as $tag => $value) {
$this->template = str_replace('{'.$tag.'}', $value, $this->template);
}
return true;
}
}