Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot Other languages Spanish/Español Programación [GUÍA] Administrador de Templates simples PHP

 
  • 0 Vote(s) - 0 Average
[GUÍA] Administrador de Templates simples PHP
DarkThinking
Offline

Burgershot Member
Posts: 3
Threads: 1
Joined: May 2019
Reputation: 0
Location: TU VIEJA 2019
#1
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:

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

} 
Diego_Estefano
Offline

Burgershot Member
Posts: 1
Threads: 0
Joined: May 2019
Reputation: 0
Location: Narnya
#2
2019-05-30, 05:20 PM
Perfecto, entendì todo a la perfecciòn, +2019
qwerty
Offline

Banned
Posts: 61
Threads: 3
Joined: Apr 2019
#3
2019-06-01, 01:58 PM
sisi, mejor uso smarty
iframe
Offline

Burgershot Member
Posts: 1
Threads: 0
Joined: Jun 2019
Reputation: 0
Location: localhost
#4
2019-06-02, 09:39 AM
Pensaba que smarty estaba extinto y ya no lo usa nadie.
qwerty
Offline

Banned
Posts: 61
Threads: 3
Joined: Apr 2019
#5
2019-06-02, 01:47 PM
(2019-06-02, 09:39 AM)iframe Wrote: Pensaba que smarty estaba extinto y ya no lo usa nadie.

pero aún sirve y a veces te responden en los foros. Mejor que esta garcha que te falla cada 3 segundos buijijiji
DarkThinking
Offline

Burgershot Member
Posts: 3
Threads: 1
Joined: May 2019
Reputation: 0
Location: TU VIEJA 2019
#6
2019-06-13, 10:59 PM
(2019-06-02, 01:47 PM)qwerty Wrote:
(2019-06-02, 09:39 AM)iframe Wrote: Pensaba que smarty estaba extinto y ya no lo usa nadie.

pero aún sirve y a veces te responden en los foros. Mejor que esta garcha que te falla cada 3 segundos buijijiji

No falla, aparte es un query builder, 
lo unico que hace es permitirte hacer de una forma "mas elegante" el query


y si no te gusta no tienes porque comentar saludos.
Keizer
Offline

Comunidad LatinoAmerica [BURGERSHOT]
Posts: 60
Threads: 3
Joined: Apr 2019
Reputation: 0
Location: Argentina
#7
2019-06-27, 07:36 PM
Alv no entiendo nada xd
Mugsy Away

Desarollador
Posts: 171
Threads: 10
Joined: Apr 2019
Reputation: 0
Location: Spain
#8
2019-06-27, 11:51 PM
Muchísimas gracias.
[Image: oN9R4KR.gif]
qwerty
Offline

Banned
Posts: 61
Threads: 3
Joined: Apr 2019
#9
2019-06-29, 08:49 PM
es literalmente lo mismo que twig y smarty pero hecho por un latinoide y sin soporte de foreach, forelse, if, etcétera
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode