begin process at 2010 03 21 17:10:31
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Class et Objet ( POO )

 > CLASS TEMPLATE

CLASS TEMPLATE


 Description

Simple CLASS pour templates HTML

Source

  • class templates
  • {
  • var $nom;
  • var $html;
  • var $fields;
  • var $loop;
  • var $bloc;
  • function templates($nom,$dossier="tpl/",$extend=".tpl")
  • {
  • $this->nom = $dossier.$nom.$extend;
  • if(file_exists($this->nom))
  • {
  • if(!$pfichier = fopen($this->nom, "r"))
  • {
  • $this->halt_template("Impossible d’ouvrir le fichier.");
  • }
  • $this->html = fread($pfichier, filesize($this->nom));
  • fclose ($pfichier);
  • if(get_magic_quotes_runtime() == 1)
  • {
  • stripslashes($this->html);
  • }
  • } else {
  • $this->halt_template("Le fichier n’existe pas.");
  • }
  • $this->fields = array();
  • $this->loop = array();
  • }
  • function assign($box_name,$field,$value,$loop=1)
  • {
  • if((!is_array($field)) && (!is_array($value)))
  • {
  • $this->fields[$box_name][$field] = $value;
  • $this->loop[$box_name][$field] = $loop;
  • } else {
  • while($new_field=array_shift($field))
  • {
  • $this->fields[$box_name][$new_field] = array_shift($value);
  • $this->loop[$box_name][$new_field] = $loop;
  • }
  • }
  • }
  • function write_template($box_name)
  • {
  • while ($field = array_shift($this->fields))
  • {
  • $size = count($field);
  • $loop = array_shift($this->loop);
  • while($size)
  • {
  • $key = key($field);
  • $value = array_shift($field);
  • if($vloop = array_shift($loop))
  • {
  • $this->html = preg_replace("(%%".$key."%%)",$value,$this->html,$vloop);
  • } else {
  • $this->html = preg_replace("(%%".$key."%%)",$value,$this->html);
  • }
  • $size--;
  • }
  • }
  • }
  • function write_bloc($bloc_name)
  • {
  • $bloc_tmp = $this->html;
  • while ($field = array_shift($this->fields))
  • {
  • $size = count($field);
  • $loop = array_shift($this->loop);
  • while($size)
  • {
  • $key = key($field);
  • $value = array_shift($field);
  • if($vloop = array_shift($loop))
  • {
  • $bloc_tmp = preg_replace("(%%".$key."%%)",$value,$bloc_tmp,$vloop);
  • } else {
  • $bloc_tmp = preg_replace("(%%".$key."%%)",$value,$bloc_tmp);
  • }
  • $size--;
  • }
  • }
  • $this->bloc .= $bloc_tmp;
  • }
  • function clear_template($value=' ')
  • {
  • $this->html = preg_replace("(%%[0-9a-zA-Z_-]*%%)",$value,$this->html);
  • }
  • function empty_bloc()
  • {
  • $this->bloc = "";
  • }
  • function halt_template($message)
  • {
  • die ("$message : Templates session halted.");
  • }
  • }
class templates
{
        var $nom;
        var $html;
		var $fields;
		var $loop;
		var $bloc;
		
        function templates($nom,$dossier="tpl/",$extend=".tpl")
        {
                
                $this->nom	=	$dossier.$nom.$extend;
                
                if(file_exists($this->nom))
                {
                if(!$pfichier        =        fopen($this->nom, "r"))
                {
                $this->halt_template("Impossible d’ouvrir le fichier.");
                }
                $this->html        =        fread($pfichier, filesize($this->nom));
				
                fclose ($pfichier);
                
                if(get_magic_quotes_runtime() == 1)
                {
                	
                	stripslashes($this->html);
                	
                }
				
                } else {
                
                $this->halt_template("Le fichier n’existe pas.");
                
                }
                
        		$this->fields	=	array();
        		
        		$this->loop	=	array();
        		
        }
        function	assign($box_name,$field,$value,$loop=1)
        {
        	if((!is_array($field)) && (!is_array($value)))
        	{
        	
        	$this->fields[$box_name][$field]	=	$value;
        	
        	$this->loop[$box_name][$field]	=	$loop;
        	
        	} else {
        		
        		while($new_field=array_shift($field))
        		{
        			$this->fields[$box_name][$new_field]	=	array_shift($value);
        			
        			$this->loop[$box_name][$new_field]	=	$loop;
        			
        		}
        	}
        }
        function	write_template($box_name)
        {
        	
			while ($field	=	array_shift($this->fields))
        	{
        		$size	=	count($field);
        		
        		$loop	=	array_shift($this->loop);
        		
        		while($size)
        		{
        		$key	=	key($field);
        		
        		$value	=	array_shift($field);
        		
        		if($vloop	=	array_shift($loop))
        		{
        		
       			$this->html	=	preg_replace("(%%".$key."%%)",$value,$this->html,$vloop);
       			
       			} else {
       			
       			$this->html	=	preg_replace("(%%".$key."%%)",$value,$this->html);
       			
       			}
       			$size--;
       			
       			}
        	}
        }
        function	write_bloc($bloc_name)
        {
        	$bloc_tmp	=	$this->html;
        	
			while ($field	=	array_shift($this->fields))
        	{
        		$size	=	count($field);
        		
        		$loop	=	array_shift($this->loop);
        		
        		while($size)
        		{
        			$key	=	key($field);
        			
        			$value	=	array_shift($field);
        			
        			if($vloop	=	array_shift($loop))
        			{
        			
       				$bloc_tmp	=	preg_replace("(%%".$key."%%)",$value,$bloc_tmp,$vloop);
       				
       				} else {
       				
       				$bloc_tmp	=	preg_replace("(%%".$key."%%)",$value,$bloc_tmp);
       				
       				}
       				
       			$size--;
       			
       			}
       			
        	}
        	
        	$this->bloc	.=	$bloc_tmp;
        	
        }
        function	clear_template($value=' ')
        {
        	
        	$this->html	=	preg_replace("(%%[0-9a-zA-Z_-]*%%)",$value,$this->html);
        	
        }
        function	empty_bloc()
        {
        	
        	$this->bloc	=	"";
        	
        }
		function	halt_template($message)
		{
			die	("$message	:	Templates session halted.");
		}
}

 Conclusion

Voici une simple class pour les templates HTML,

Création $variable = new templates('nom_du_template',$dossier,$extend);

($ extend est facultatif c'est l'extension du nom de template ex: haut.tpl
$extend = 'tpl' pour dossier default='tpl/')

Assigner ou preparer les variables du templates

$variable->assign('haut',$fields,$value ,$loop);

$fields c'est le champ à remplacer ex: %%var%% dans le tamplate

$value = la valeur

$loop = nombres de remplacement pour le champ default=1

($fields et $value peuvent etre des tableaux)

$variable->write_template('haut');

cha nge tous les champs , précédament assigner

$variable->clear_template()

pour effacer les %%variable%% du template

$variable->write_bloc('haut');

ajoute $variable->html -> $variable->bloc puis change les valeurs ,

$variable->empty_bloc('haut') vide le bloc

je sui pas un specialiste des regexp mais j'ai fait de mon mieux , enfin je l'utilise
toujours pour mes sites il fonctionne tres bien

a derniere chose quand vous faite un assign avec des tableaux , le write_template
LE VIDE!!







 Sources de la même categorie

Source avec Zip CLASSE TABLE HTML DYNAMIQUE par LDDL
Source avec Zip GÉNÉRATEUR DE COUCHE DAO POUR SITE WEB À PARTIR D'UNE BASE D... par alexfool
Source avec Zip [PHP5.2] CLASSE PDO par hornetbzz
Source avec Zip POO - LOGGING PACKAGE par Waredan
POO - OBJECT CLASS par Waredan

Commentaires et avis

Commentaire de bombleague le 08/06/2004 23:50:25

Désolé j'ai oublié, donc

apres avoir declarer ,assigner les variables , writer les valeurs il suffit
de faire un echo $variable->html; pour afficher le template modifier
à propos des bloc , $variable->html; contient le modéle $variable->bloc contient les bloc empilé & modifier LOL.

Commentaire de willinfeo le 09/06/2004 11:57:01

salut,
j'ai pas regarde avec precision ton script.
a quoi ca sert au juste.

merci

Commentaire de bombleague le 09/06/2004 14:46:37

voici un exemple

crée un rep tpl

créer le fichier test.tpl dedans (c'est un fichier html en faite)

fichier : test.tpl

<html>
<head>
<title>%%title%%</title>
</head>
<body>
<p>Welcome %%nom%%</p>
</body>
</Html>

créer le fichier test.php sur la racine pas dans tpl/
fichier : test.php

<?php
# ici fait un include ou un require ou copie directement la class template

$test=new templates('test');

$test->assign('test','title','Bienvenue sur mon site');

$test->assign('test','nom','martin');

$test->write-template('test');

echo $test->html;

exit();
?>

Commentaire de phpathome le 09/06/2004 16:53:08

oui mais tu gère pas les <table> avec ça, par exemple pour les résultats d'une requête SQL...

Commentaire de astuces_jeux le 01/08/2008 22:09:53

<?php
class templates
{
        var $nom;
        var $html;
var $fields;
var $loop;
var $bloc;

        function templates($nom,$dossier="templates/",$extend=".tpl")
        {
                
                $this->nom = $dossier.$nom.$extend;
                
                if(file_exists($this->nom))
                {
                if(!$pfichier        =        fopen($this->nom, "r"))
                {
                $this->halt_template("Impossible d&rsquo;ouvrir le fichier.");
                }
                $this->html        =        fread($pfichier, filesize($this->nom));

                fclose ($pfichier);
                
                if(get_magic_quotes_runtime() == 1)
                {
                
                 stripslashes($this->html);
                
                }

                } else {
                
                $this->halt_template("Le fichier n&rsquo;existe pas.");
                
                }
                
         $this->fields = array();
        
         $this->loop = array();
        
        }
        function assign($box_name,$field,$value,$loop=1)
        {
         if((!is_array($field)) && (!is_array($value)))
         {
        
         $this->fields[$box_name][$field] = $value;
        
         $this->loop[$box_name][$field] = $loop;
        
         } else {
        
         while($new_field=array_shift($field))
         {
         $this->fields[$box_name][$new_field] = array_shift($value);
        
         $this->loop[$box_name][$new_field] = $loop;
        
         }
         }
        }
        function write_template($box_name)
        {
        
while ($field = array_shift($this->fields))
         {
         $size = count($field);
        
         $loop = array_shift($this->loop);
        
         while($size)
         {
         $key = key($field);
        
         $value = array_shift($field);
        
         if($vloop = array_shift($loop))
         {
        
       $this->html = preg_replace("(<".$key.">)",$value,$this->html,$vloop);
      
       } else {
      
       $this->html = preg_replace("(<".$key.">)",$value,$this->html);
      
       }
       $size--;
      
       }
         }
        }
        function write_bloc($bloc_name)
        {
         $bloc_tmp = $this->html;
        
while ($field = array_shift($this->fields))
         {
         $size = count($field);
        
         $loop = array_shift($this->loop);
        
         while($size)
         {
         $key = key($field);
        
         $value = array_shift($field);
        
         if($vloop = array_shift($loop))
         {
        
       $bloc_tmp = preg_replace("(<".$key.">)",$value,$bloc_tmp,$vloop);
      
       } else {
      
       $bloc_tmp = preg_replace("(<".$key.">)",$value,$bloc_tmp);
      
       }
      
       $size--;
      
       }
      
         }
        
         $this->bloc .= $bloc_tmp;
        
        }
        function clear_template($value='&nbsp;')
        {
        
         $this->html = preg_replace("(<[0-9a-zA-Z_-]*>)",$value,$this->html);
        
        }
        function empty_bloc()
        {
        
         $this->bloc = "";
        
        }
function halt_template($message)
{
die ("$message : Templates session halted.");
}
}
$test=new templates('test');
$test->assign('test','php','<?php');
$test->assign('test','/php','?>');
$test->assign('test','include','include "');
$test->assign('test','/include','";');
$test->write_template('test');

echo $test->html;

exit();
?>

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Mars 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,359 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales