• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[GU?A] Administrador de Templates simples PHP
#1
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:

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; ? ?} }
  Reply


Messages In This Thread
[GU?A] Administrador de Templates simples PHP - by DarkThinking - 2019-05-30, 05:16 PM