Systeme de template perso, j'ai rédigé un tuto qui présente les principales fonctions :
http://s219717426.onlinehome.fr/cc/Template/
Pour télécharger le zip avec tuto et exemple :
http://s219717426.onlinehome.fr/cc/Template/Template.zip
Source / Exemple :
<?php
/*
*
- @author François Germain <cahnory@gmail.com>
- @version 0.1.4
*
*
- Valide un formulaire en fonction du type de champs attendu
- @var array template_filename L'adresse du fichier de template
- @var array template_path Le dossier contenant le template
- @var array template_name Le nom du template
- @var array compiled_code Le code compilé (en php)
- @var array vars Les variables du templates
- @var boolean cache Activation du cache
- @var string compilation_folder Dossier de cache
- @param string compilation_folder Dossier où sont stockés les fichier de cache
- NULL désactive le cache
*
- Cette création est mise à disposition selon le Contrat
- Paternité-Partage des Conditions Initiales à l'Identique
- 2.0 France disponible en ligne
- http://creativecommons.org/licenses/by-sa/2.0/fr/
- ou par courrier postal à
- Creative Commons, 171 Second Street,
- Suite 300, San Francisco, California 94105, USA.
*
class TempLAX
{
private $template_filename;
private $template_content;
private $template_path;
private $template_name;
private $compiled_code;
private $vars;
private $stack = array();
private $cache = false;
private $compilation_folder;
private $debug;
public function __construct($compilation_folder = NULL, $debug = false)
{
$this->vars['.'] = array();
$this->vars['block'] = array();
$this->vars['form'] = array();
if(is_dir($compilation_folder)){
$this->cache = true;
$this->compilation_folder = $compilation_folder;
}
$this->debug = $debug;
}
/**
- Défini le fichier template à utiliser.
- @param string template_filename L'adresse du fichier
- /
public function set_file($template_filename = NULL)
{
if(is_file($template_filename)){
$this->template_filename = $template_filename;
$this->template_path = preg_replace('#([.]*)([^/]*)$#','$1',$template_filename);
$this->template_name = preg_replace('#([.]*)([^/]*)$#','$2',$template_filename);
$this->compiled_filename = $this->compilation_folder.md5($template_filename).'.php';
$this->template_content = file_get_contents($this->template_filename);
}elseif($this->debug){
echo 'TempLAX->set_file() : Le fichier de template est introuvable';
}
}
/**
- Défini le fichier template à utiliser.
- @param string template_filename L'adresse du fichier
- /
public function content($content = NULL)
{
if(is_null($content)){
return $this->template_content;
}elseif($this->debug){
$this->template_content = $content;
return $this->template_content;
}
}
/**
- Remplis une variable $name avec $value
- @param string name Le nom de la variable
- @param mixed value Le contenu de la variable
- /
public function set_var($name, $value = NULL)
{
$this->vars['.'][$name] = $value;
}
/**
- Remplis des variable à l'aide d'un tableau indexé name=>value
- @param array vars Le tableau contenant les variables
- /
/**
- Remplis des variable à l'aide d'un tableau indexé name=>value
- @param array vars Le tableau contenant les variables
- /
public function set_vars($vars = array())
{
$this->vars['.'] = array_merge($this->vars['.'],$vars);
}
// Sera prochainement supprimé
public function set_avar($vars = array())
{
$this->vars['.'] = array_merge($this->vars['.'],$vars);
}
/**
- Remplis un bloc $block avec ses variables $vars (tableau indexé
- name=>value). Si $n est spécifié, les variables du énième bloc $block
- précédemment remplis sont modifiées
- @param string block Le nom du block
- @param array vars Variables du block
- @param numeric n Numéro de block
- /
public function set_block($block = NULL, $vars = array(), $n = NULL)
{
$underBlock = preg_replace('#([.])?([[:alnum:]_\-]+)$#','',$block);
if(isset($this->vars['block'][$underBlock]))
{
$un = sizeOf($this->vars['block'][$underBlock]) - 1;
$nb = sizeOf($this->vars['block'][$underBlock][$un]) - 1;
}else{
$nb = 0;
}
if(!isset($this->vars['block'][$block])) $this->vars['block'][$block][0] = array();
if($n === NULL){
$this->vars['block'][$block][$nb][] = $vars;
}else{
$this->vars['block'][$block][$nb][$n] = array_merge($this->vars['block'][$block][$n],$vars);
}
}
/**
- Remplis un bloc $block avec ses variables $vars (tableau indexé
- name=>value). Si $n est spécifié, les variables du énième bloc $block
- précédemment remplis sont modifiées
- @param string block Le nom du block
- @param array vars Variables du block
- @param numeric n Numéro de block
- /
public function set_form($form, $values = array(), $errors = array())
{
if(!isset($this->vars['form'][$form])){
$this->vars['form'][$form] = array();
$this->vars['form'][$form]['values'] = $values;
$this->vars['form'][$form]['errors'] = $errors;
}else{
$this->vars['form'][$form]['values'] = array_merge($this->vars['form'][$form]['values'],$values);
$this->vars['form'][$form]['errors'] = array_merge($this->vars['form'][$form]['errors'],$errors);
}
}
/**
- Compile et execute le fichier template
- @return string Le contenu compilé du template
- /
public function parse()
{
ob_start();
$this->i = null;
$this->n = null;
$this->block = null;
$this->stack[] = array(
'i' => $this->i,
'n' => $this->n,
'block' => $this->block
);
if($this->cache){
if(!is_file($this->compiled_filename) OR $this->debug) $this->cache_template();
include($this->compiled_filename);
}else{
$this->compil();
eval('?>'.$this->compiled_code);
}
return ob_get_clean();
}
/**
- Compile le fichier template, les commentaires html utilisés
- par le moteur de template sont convertis en code php.
- /
private function compil()
{
$output = $this->template_content;
// INSERT
$output = preg_replace_callback('#<!--INS(ERT)? ([^>]+) -->#', array(get_class($this),'insert'),$output);
// CODE
$output = preg_replace_callback('#(?:<!--CODE -->)((\n|\r|.)*?)(?:<!-- CODE-->)#', array(get_class($this),'code'),$output);
// INCLUDE
$output = preg_replace('#<!--INC(LUDE)? (?!./)([^>]+) -->#','<?php include(\''.$this->template_path.'$2\') ?>',$output);
$output = preg_replace('#<!--INC(LUDE)? ([^>]+) -->#','<?php include(\'$2\') ?>',$output);
// BLOCK
$output = preg_replace('#<!--BLOCK ([[:alnum:]._\-]+) -->#','<?php $this->startBlock(\'$1\'); for($this->i = 0; $this->i<($this->n = sizeof(@$this->vars[\'block\'][\'$1\'][0])); $this->i++): ?>',$output);
$output = preg_replace('#<!--(END)? BLOCK-->#','<?php endfor; $this->endBlock(); ?>',$output);
// BLOCK > ROW
$output = preg_replace('#<!--ROW((.(?!-->))*) -->#','<?php echo $this->i$1 ?>',$output);
// BLOCK > NUMROWS
$output = preg_replace('#<!--NUMROWS((.(?!-->))*) -->#','<?php echo $this->n$1 ?>',$output);
// FORM
$output = preg_replace('#<!--FORM ([[:alnum:]_\-]+) -->#','<?php if(isset($this->vars[\'form\'][\'$1\'])): ?>',$output);
$output = preg_replace('#<!--(END)? FORM-->#','<?php endif; ?>',$output);
// FORM > VALUE
$output = preg_replace_callback('#<!--VALUE ([[:alnum:]_\-]+)\.([[:alnum:]\[\]_\-]+) -->#', array(get_class($this),'value'),$output);
// FORM > VALID
$output = preg_replace('#<!--(!)?VALID ([[:alnum:]_\-]+) -->#','<?php if(sizeof($this->vars[\'form\'][\'$2\'][\'errors\']) $1== 0): ?>',$output);
$output = preg_replace('#<!--(!)?VALID ([[:alnum:]._\-]+)\.([[:alnum:]\[\]_\-]+) -->#','<?php if(in_array(\'$3\',$this->vars[\'form\'][\'$2\'][\'errors\']) $1== false): ?>',$output);
$output = preg_replace('#<!--(!)?VALID ([[:alnum:]._\-]+)\.([[:alnum:]_\-]+) -->#','<?php if(in_array(\'$3\',$this->vars[\'form\'][\'$2\'][\'errors\']) $1== false): ?>',$output);
$output = preg_replace('#<!--(END)? VALID-->#','<?php endif; ?>',$output);
// FORM > ERRORS
$output = preg_replace('#<!--ERRORS ([[:alnum:]_\-]+) -->#','<?php echo sizeof($this->vars[\'form\'][\'$1\'][\'errors\']) ?>',$output);
// VAR
$output = preg_replace('#<!--VAR ([[:alnum:]_\-]+) -->#','<?php echo @$this->vars[\'.\'][\'$1\'] ?>',$output);
$output = preg_replace('#<!--VAR ([[:alnum:]._\-]+)\.([[:alnum:]_\-]+) -->#','<?php echo @$this->vars[\'block\'][\'$1\'][0][$this->i][\'$2\'] ?>',$output);
// ISSET
$output = preg_replace('#<!--(!)?ISSET ([[:alnum:]_\-]+) -->#','<?php if($1isset($this->vars[\'.\'][\'$2\'])): ?>',$output);
$output = preg_replace('#<!--(!)?ISSET ([[:alnum:]._\-]+)\.([[:alnum:]_\-]+) -->#','<?php if($1isset($this->vars[\'block\'][\'$2\'][0][$this->i][\'$3\'])): ?>',$output);
$output = preg_replace('#<!--(END)? ISSET-->#','<?php endif; ?>',$output);
// EMPTY
$output = preg_replace('#<!--(!)?EMPTY ([[:alnum:]_\-]+) -->#','<?php if($1isset($this->vars[\'.\'][\'$2\']) || $1empty($this->vars[\'.\'][\'$2\'])): ?>',$output);
$output = preg_replace('#<!--(!)?EMPTY ([[:alnum:]._\-]+)\.([[:alnum:]_\-]+) -->#','<?php if($1empty($this->vars[\'block\'][\'$2\'][0][$this->i][\'$3\'])): ?>',$output);
$output = preg_replace('#<!--(END)? EMPTY-->#','<?php endif; ?>',$output);
// DATE
$output = preg_replace('#<!--DATE((.(?!-->))*) -->#','<?php echo date("$1") ?>',$output);
// COLOR
$output = preg_replace_callback('#(?:<!--COLOR -->)((\n|\r|.)*?)(?:<!-- COLOR-->)#', array(get_class($this),'color'),$output);
$this->compiled_code = $output;
}
/**
- Compile le template et le met en cache, le nom du fichier
- compilé est le hash md5 de l'adresse du fichier afin qu'il
- soit unique.
- /
private function cache_template()
{
$this->compil();
$compiled_file = fopen($this->compiled_filename, "w");
fseek ($compiled_file, 0);
fwrite ($compiled_file, $this->compiled_code);
fclose ($compiled_file);
}
private function insert($matches)
{
if(preg_match('#^./#',$matches[2]))
{
return file_get_contents($matches[2]);
}else{
return file_get_contents($this->template_path.$matches[2]);
}
}
private function value($matches)
{
return '<?php echo @$this->vars[\'form\'][\''.$matches[1].'\'][\'values\']'.$this->getFormVar($matches[2]).' ?>';
}
private function code($matches)
{
return '<?php echo \''.addslashes(htmlspecialchars($matches[1])).'\' ?>';
}
private function color($matches)
{
return '<?php highlight_string(\''.addslashes($matches[1]).'\') ?>';
}
/**
- Renvois le contenu d'une variable php en fonction de son attribut html 'name'
- @param string field L'attribut 'name' du champs à vérifier
- @param boolean file Le champs est du type file
- @return mixed
- /
private function getFormVar($field)
{
$field = preg_replace('#^([^\[]+){1}#i',"[$1]",$field);
$field = preg_replace('#(\[([^\]]+)\])#i',"['$2']",$field);
return $field;
}
private function startBlock($block)
{
$i = $this->i;
$n = $this->n;
if(!isset($this->vars['block'][$block]) || !isset($this->vars['block'][$block][0]))
{
$this->vars['block'][$block] = array(array());
}
$this->stack[] = compact('block','i','n');
}
private function endBlock()
{
if(isset($this->vars['block'][$this->stack[count($this->stack)-1]['block']])){
array_splice($this->vars['block'][$this->stack[count($this->stack)-1]['block']],0,1);
extract($this->stack[count($this->stack)-1]);
$this->i = $i;
$this->n = $n;
$this->block = $block;
array_splice($this->stack,count($this->stack)-1,1);
}
}
}
?>
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.