TUTORIAL: Fully Functional simple moteur de template (Suite)
- Bogey
- Bogey


- Inscription: Juil 14, 2005
- Messages: 8211
- Loc: USA
- Status: Offline
Introduction
Ce tutoriel s'étend sur le didacticiel précédent de la Entièrement fonctionnelle moteur modèle simple . Il y aurait certaines fonctions édition et je voudrais enfin vous fournir les capacités de mise en cache ici.
Avant de commencer à ce sujet, permettez-moi de confesser...la classe de mise en cache sur le point de libérer Im n'est pas écrit par moi...Je l'ai trouvé quelque part sur OZZU et supposé que c'était ok pour moi d'utiliser...car il fonctionne exactement comme je l'ai besoin, je n'ai pas trouvé nécessaire de recoder moi-même un autre, même si je pouvais (je vous promets...Je ne peux vraiment gif "alt =": lol: "title =" Laughing "> )
Ce tutoriel n'est pas, par tout moyen, le dernier tutoriel sur la base de cette simple entièrement fonctionnel Template Engine...quand je fais assez de modifications à la classe pour remplir une grande page avec les modifications et si elle vous apprend quelque chose...puis après un autre mauvais ici...mais il pourrait être le dernier gif "alt =": lol: "title =" Laughing ">
Ajouté
Commençons par l'ajout de certaines fonctions plus ici: La fonction est appelée include_file .
PHP Code: [ Select ]
<?php
function include_file($file)
{
}
?>
function include_file($file)
{
}
?>
- <?php
- function include_file($file)
- {
- }
- ?>
La raison étant est que, parfois, vous voulez mettre un nom de fichier doit être réglé sur une variable, mais vous ne voulez pas en fait d'inclure le contenu de celui-ci dans le modèle...alors j'ai fait une solution simple ici...pour chaque fichier Vous ne voulez pas inclure dans le fichier, il suffit d'ajouter une * en face d'elle...le * est parfait parce qu'il n'est pas autorisé dans le nom du fichier à être activé lorsque vous créez un fichier et il nous permet de créer cette fonctionnalité
PHP Code: [ Select ]
<?php
function include_file($file)
{
// Taking the first character from the filename
$find = substr($file, 0, 1);
// Checking if * exists in $filename
if($find != '*')
{
return true;
}
return false;
}
?>
function include_file($file)
{
// Taking the first character from the filename
$find = substr($file, 0, 1);
// Checking if * exists in $filename
if($find != '*')
{
return true;
}
return false;
}
?>
- <?php
- function include_file($file)
- {
- // Taking the first character from the filename
- $find = substr($file, 0, 1);
- // Checking if * exists in $filename
- if($find != '*')
- {
- return true;
- }
- return false;
- }
- ?>
PHP Code: [ Select ]
<?php
function include_file($file)
{
// Checking if * exists in $filename
if(strpos($file, '*') != false)
{
return true;
}
return false;
}
?>
function include_file($file)
{
// Checking if * exists in $filename
if(strpos($file, '*') != false)
{
return true;
}
return false;
}
?>
- <?php
- function include_file($file)
- {
- // Checking if * exists in $filename
- if(strpos($file, '*') != false)
- {
- return true;
- }
- return false;
- }
- ?>
La fonction suivante est ce que j'ai nommé à unclude_file . Elle n'a en général ce que la fonction ci-dessus ne, mais il élimine aussi l'astérisque pour l'assainissement.
PHP Code: [ Select ]
<?php
function unclude_file($file, $perform = false)
{
// Checking if the file was passed as an un-includeable
if($this->include_file($file) === false)
{
// Checking if we should actually remove the first character (It being the '*')
if($perform == true)
{
// Counting the number of letters $file consists of
$ltrs = strlen($file);
// Returning the trimmed $file
return substr($file, 1, $ltrs);
}
return true;
}
return false;
}
?>
function unclude_file($file, $perform = false)
{
// Checking if the file was passed as an un-includeable
if($this->include_file($file) === false)
{
// Checking if we should actually remove the first character (It being the '*')
if($perform == true)
{
// Counting the number of letters $file consists of
$ltrs = strlen($file);
// Returning the trimmed $file
return substr($file, 1, $ltrs);
}
return true;
}
return false;
}
?>
- <?php
- function unclude_file($file, $perform = false)
- {
- // Checking if the file was passed as an un-includeable
- if($this->include_file($file) === false)
- {
- // Checking if we should actually remove the first character (It being the '*')
- if($perform == true)
- {
- // Counting the number of letters $file consists of
- $ltrs = strlen($file);
- // Returning the trimmed $file
- return substr($file, 1, $ltrs);
- }
- return true;
- }
- return false;
- }
- ?>
Aussi, je ajouter une variable à l'ensemble de la classe appelée include_files . Il détient vrai si vous voulez que les fichiers qui sont passés dans la définition variable. être inclus dans le modèle (comme dans leur contenu) ou non inclus dans les modèles (Seuls les noms de fichiers seront inclus...pas la source).
PHP Code: [ Select ]
<?php
class tpl {
// Various variables used throughout the class
public $vari, $template, $page, [color=#0000FF]$vstart, $vend, $include_files;
}
?>
class tpl {
// Various variables used throughout the class
public $vari, $template, $page, [color=#0000FF]$vstart, $vend, $include_files;
}
?>
- <?php
- class tpl {
- // Various variables used throughout the class
- public $vari, $template, $page, [color=#0000FF]$vstart, $vend, $include_files;
- }
- ?>
Édité
A droite, je l'ai dit, j'ai édité à certaines fonctions, permet ainsi obtenir à ce sujet gif "title =":)" alt =" Smile ">
La première fonction que j'ai édité a été le svariable pour tenir compte des include_file () fonction et la unclude_file () fonction. Ici, il est dans toute sa splendeur
PHP Code: [ Select ]
<?php
function svariable($return = false)
{
// Converting set variables to the text defined in the parent PHP file
foreach($this->vari as $key => $value)
{
// Checking if the value is a file and if it exists if it is a file
if(is_file($value) && file_exists($value))
{
if($this->include_files === true && ($this->include_file($value) === true))
{
// Getting the contents of the file
$value = file_get_contents($value);
// Replacing the KEY variable with the contents of the file
$this->text = str_replace($this->vstart . $key . $this->vend, $value, $this->text);
}
else
{
// Replacing the KEY variable with the value
$this->text = str_replace($this->vstart . $key . $this->vend, $value,$this->text);
}
}
else
{
if($this->unclude_file($value) === true)
{
$value = $this->unclude_file($value, true);
}
// Replacing the KEY variable with the value
$this->text = str_replace($this->vstart . $key . $this->vend, $value,$this->text);
}
}
// Checking if we need to return the result
if($return)
{
return $this->text;
}
}
?>
function svariable($return = false)
{
// Converting set variables to the text defined in the parent PHP file
foreach($this->vari as $key => $value)
{
// Checking if the value is a file and if it exists if it is a file
if(is_file($value) && file_exists($value))
{
if($this->include_files === true && ($this->include_file($value) === true))
{
// Getting the contents of the file
$value = file_get_contents($value);
// Replacing the KEY variable with the contents of the file
$this->text = str_replace($this->vstart . $key . $this->vend, $value, $this->text);
}
else
{
// Replacing the KEY variable with the value
$this->text = str_replace($this->vstart . $key . $this->vend, $value,$this->text);
}
}
else
{
if($this->unclude_file($value) === true)
{
$value = $this->unclude_file($value, true);
}
// Replacing the KEY variable with the value
$this->text = str_replace($this->vstart . $key . $this->vend, $value,$this->text);
}
}
// Checking if we need to return the result
if($return)
{
return $this->text;
}
}
?>
- <?php
- function svariable($return = false)
- {
- // Converting set variables to the text defined in the parent PHP file
- foreach($this->vari as $key => $value)
- {
- // Checking if the value is a file and if it exists if it is a file
- if(is_file($value) && file_exists($value))
- {
- if($this->include_files === true && ($this->include_file($value) === true))
- {
- // Getting the contents of the file
- $value = file_get_contents($value);
- // Replacing the KEY variable with the contents of the file
- $this->text = str_replace($this->vstart . $key . $this->vend, $value, $this->text);
- }
- else
- {
- // Replacing the KEY variable with the value
- $this->text = str_replace($this->vstart . $key . $this->vend, $value,$this->text);
- }
- }
- else
- {
- if($this->unclude_file($value) === true)
- {
- $value = $this->unclude_file($value, true);
- }
- // Replacing the KEY variable with the value
- $this->text = str_replace($this->vstart . $key . $this->vend, $value,$this->text);
- }
- }
- // Checking if we need to return the result
- if($return)
- {
- return $this->text;
- }
- }
- ?>
Le cache
Bon, depuis que j'ai pris cette classe de quelqu'un sur ce forum, malade juste après toute la classe ensemble ici (sans coloration en rouge...J'ai de colorer chaque ligne séparément...).
Ce qu'il fait bien, est crée un fichier avec des variables non remplacé...il inclut tous les fichiers d'abord, puis le met en cache. L'identifiant (ou le nom qu'il serait enregistré sur) est le nom du fichier...si vous utilisez un modèle plus d'une fois sur les fichiers PHP différents...ne pas mettre en cache
PHP Code: [ Select ]
<?php
class fcache {
var $name = NULL; // private members only >= PHP 5
var $value = array();
var $ttl;
function __construct($name, $ttl = 3600) { // Default $ttl is 3600 (60 minutes until expiry)
$this->name = $name;
$this->ttl = $ttl;
}
function check() {
$cached = false;
$file_name = './cache/' . $this->name . ".cache";
if(file_exists($file_name))
{
$modified = filemtime($file_name);
if(time() - $this->ttl < $modified)
{
$fp = fopen($file_name, "rt");
if($fp)
{
$temp_value = fread($fp, filesize($file_name));
fclose($fp);
$this->value = unserialize($temp_value);
$cached = true;
}
}
else
{
$this->del_value($this->name);
$this->set_value($this->name, $this->value);
$this->save();
}
}
return $cached;
}
function save() {
$file_name = './cache/' . $this->name . ".cache";
$fp = fopen($file_name, "wt");
if($fp)
{
fwrite($fp, serialize($this->value));
fclose($fp);
}
}
function set_value($key, $value) {
$this->value[$key] = $value;
}
function get_value($key) {
if(isset($this->value[$key]))
{
return $this->value[$key];
}
else
{
return null;
}
}
function del_value($key)
{
$file_name = './cache/' . $this->name . ".cache";
if(file_exists($file_name))
{
unset($this->value[$key]);
return unlink($file_name);
}
return false;
}
}
?>
class fcache {
var $name = NULL; // private members only >= PHP 5
var $value = array();
var $ttl;
function __construct($name, $ttl = 3600) { // Default $ttl is 3600 (60 minutes until expiry)
$this->name = $name;
$this->ttl = $ttl;
}
function check() {
$cached = false;
$file_name = './cache/' . $this->name . ".cache";
if(file_exists($file_name))
{
$modified = filemtime($file_name);
if(time() - $this->ttl < $modified)
{
$fp = fopen($file_name, "rt");
if($fp)
{
$temp_value = fread($fp, filesize($file_name));
fclose($fp);
$this->value = unserialize($temp_value);
$cached = true;
}
}
else
{
$this->del_value($this->name);
$this->set_value($this->name, $this->value);
$this->save();
}
}
return $cached;
}
function save() {
$file_name = './cache/' . $this->name . ".cache";
$fp = fopen($file_name, "wt");
if($fp)
{
fwrite($fp, serialize($this->value));
fclose($fp);
}
}
function set_value($key, $value) {
$this->value[$key] = $value;
}
function get_value($key) {
if(isset($this->value[$key]))
{
return $this->value[$key];
}
else
{
return null;
}
}
function del_value($key)
{
$file_name = './cache/' . $this->name . ".cache";
if(file_exists($file_name))
{
unset($this->value[$key]);
return unlink($file_name);
}
return false;
}
}
?>
- <?php
- class fcache {
- var $name = NULL; // private members only >= PHP 5
- var $value = array();
- var $ttl;
- function __construct($name, $ttl = 3600) { // Default $ttl is 3600 (60 minutes until expiry)
- $this->name = $name;
- $this->ttl = $ttl;
- }
- function check() {
- $cached = false;
- $file_name = './cache/' . $this->name . ".cache";
- if(file_exists($file_name))
- {
- $modified = filemtime($file_name);
- if(time() - $this->ttl < $modified)
- {
- $fp = fopen($file_name, "rt");
- if($fp)
- {
- $temp_value = fread($fp, filesize($file_name));
- fclose($fp);
- $this->value = unserialize($temp_value);
- $cached = true;
- }
- }
- else
- {
- $this->del_value($this->name);
- $this->set_value($this->name, $this->value);
- $this->save();
- }
- }
- return $cached;
- }
- function save() {
- $file_name = './cache/' . $this->name . ".cache";
- $fp = fopen($file_name, "wt");
- if($fp)
- {
- fwrite($fp, serialize($this->value));
- fclose($fp);
- }
- }
- function set_value($key, $value) {
- $this->value[$key] = $value;
- }
- function get_value($key) {
- if(isset($this->value[$key]))
- {
- return $this->value[$key];
- }
- else
- {
- return null;
- }
- }
- function del_value($key)
- {
- $file_name = './cache/' . $this->name . ".cache";
- if(file_exists($file_name))
- {
- unset($this->value[$key]);
- return unlink($file_name);
- }
- return false;
- }
- }
- ?>
Maintenant, nous devons mettre les choses en notre gentemp .
PHP Code: [ Select ]
<?php
function gentemp($cache = false, $return = false)
{
// Converting simple function variables to function
$this->sfvariable();
// Checking if the page needs to be cached... if so, cache it.
if($cache)
{
// Setting the cache class object
$cache = new fcache($this->page);
// Checking if the cache is available for the current page
if($cache->check())
{
// Retrieving the cached contents of the page
$this->text = $cache->get_value($this->page);
}
else
{
// Setting the cache with the contents of the current page
$cache->set_value($cache->name, $this->text);
// Saving the cache
$cache->save();
// Retrieving the cached contents of the page
$this->text = $cache->get_value($this->page);
}
}
// Converting simple variables to variables
$this->svariable();
// Checking if we need to echo or return the page
if($return)
{
// Returning the page
return $this->text;
}
else
{
// Echoing the page
echo $this->text;
}
}
?>
function gentemp($cache = false, $return = false)
{
// Converting simple function variables to function
$this->sfvariable();
// Checking if the page needs to be cached... if so, cache it.
if($cache)
{
// Setting the cache class object
$cache = new fcache($this->page);
// Checking if the cache is available for the current page
if($cache->check())
{
// Retrieving the cached contents of the page
$this->text = $cache->get_value($this->page);
}
else
{
// Setting the cache with the contents of the current page
$cache->set_value($cache->name, $this->text);
// Saving the cache
$cache->save();
// Retrieving the cached contents of the page
$this->text = $cache->get_value($this->page);
}
}
// Converting simple variables to variables
$this->svariable();
// Checking if we need to echo or return the page
if($return)
{
// Returning the page
return $this->text;
}
else
{
// Echoing the page
echo $this->text;
}
}
?>
- <?php
- function gentemp($cache = false, $return = false)
- {
- // Converting simple function variables to function
- $this->sfvariable();
- // Checking if the page needs to be cached... if so, cache it.
- if($cache)
- {
- // Setting the cache class object
- $cache = new fcache($this->page);
- // Checking if the cache is available for the current page
- if($cache->check())
- {
- // Retrieving the cached contents of the page
- $this->text = $cache->get_value($this->page);
- }
- else
- {
- // Setting the cache with the contents of the current page
- $cache->set_value($cache->name, $this->text);
- // Saving the cache
- $cache->save();
- // Retrieving the cached contents of the page
- $this->text = $cache->get_value($this->page);
- }
- }
- // Converting simple variables to variables
- $this->svariable();
- // Checking if we need to echo or return the page
- if($return)
- {
- // Returning the page
- return $this->text;
- }
- else
- {
- // Echoing the page
- echo $this->text;
- }
- }
- ?>
Exemple d'utilisation
Maintenant, un exemple d'utilisation de toute cette affaire serait sur la première partie de ce tutoriel ( ??? ) Ou au-dessous
processus. php
PHP Code: [ Select ]
<?php
require('includes/globals.php');
$db->connect();
$db->last_sql = $db->build_query(array('*', 'forum_a'));
$result = $db->last_sql_data(false, 'SQL Results Data', true);
$db->close();
$tpl->template = 'default';
$tpl->page = 'index';
$tpl->start();
include('includes/global_vars.php');
$tpl->vari = array_merge($tpl->vari, array(
'SQL_QUERY' => $result,
'MEMBER_STUFF' => ((isset($_SESSION['uid'])) ? 'members.html' : 'non_members.html'),
'USERNAME' => $username,
'LAST_VISIT' => date(MM/DD/YYYY hh:mm:ss, $last_visit)
'NOW_DATE' => date(MM/DD/YYYY hh:mm:ss)
));
$tpl->gentemp(true);
?>
require('includes/globals.php');
$db->connect();
$db->last_sql = $db->build_query(array('*', 'forum_a'));
$result = $db->last_sql_data(false, 'SQL Results Data', true);
$db->close();
$tpl->template = 'default';
$tpl->page = 'index';
$tpl->start();
include('includes/global_vars.php');
$tpl->vari = array_merge($tpl->vari, array(
'SQL_QUERY' => $result,
'MEMBER_STUFF' => ((isset($_SESSION['uid'])) ? 'members.html' : 'non_members.html'),
'USERNAME' => $username,
'LAST_VISIT' => date(MM/DD/YYYY hh:mm:ss, $last_visit)
'NOW_DATE' => date(MM/DD/YYYY hh:mm:ss)
));
$tpl->gentemp(true);
?>
- <?php
- require('includes/globals.php');
- $db->connect();
- $db->last_sql = $db->build_query(array('*', 'forum_a'));
- $result = $db->last_sql_data(false, 'SQL Results Data', true);
- $db->close();
- $tpl->template = 'default';
- $tpl->page = 'index';
- $tpl->start();
- include('includes/global_vars.php');
- $tpl->vari = array_merge($tpl->vari, array(
- 'SQL_QUERY' => $result,
- 'MEMBER_STUFF' => ((isset($_SESSION['uid'])) ? 'members.html' : 'non_members.html'),
- 'USERNAME' => $username,
- 'LAST_VISIT' => date(MM/DD/YYYY hh:mm:ss, $last_visit)
- 'NOW_DATE' => date(MM/DD/YYYY hh:mm:ss)
- ));
- $tpl->gentemp(true);
- ?>
C'est vrai dans le gentemp ( dans le morceau de code ci-dessus indique le code pour le cache ainsi.
Et le fichier de modèle pourrait ressembler
HTML Code: [ Select ]
<!-- INCLUDE header.html -->
<div id="content">
<h6 id="header">Welcome {USERNAME} your last visit was {LAST_VISIT} from today ({NOW)DATE})</h6>
<p>{MEMBER_STUFF}</p>
<p>{SQL_QUERY}</p>
</div>
<!-- INCLUDE footer.html -->
<div id="content">
<h6 id="header">Welcome {USERNAME} your last visit was {LAST_VISIT} from today ({NOW)DATE})</h6>
<p>{MEMBER_STUFF}</p>
<p>{SQL_QUERY}</p>
</div>
<!-- INCLUDE footer.html -->
- <!-- INCLUDE header.html -->
- <div id="content">
- <h6 id="header">Welcome {USERNAME} your last visit was {LAST_VISIT} from today ({NOW)DATE})</h6>
- <p>{MEMBER_STUFF}</p>
- <p>{SQL_QUERY}</p>
- </div>
- <!-- INCLUDE footer.html -->
Hope qui avait du sens
Conclusion
Eh bien, ce qu'il est pour l'instant. Espérons que vous apprécierez et trouverai un bon usage pour lui
Attachments:
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Anonymous
- Bot


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Mars 4th, 2009, 4:58 pm
- Bogey
- Bogey


- Inscription: Juil 14, 2005
- Messages: 8211
- Loc: USA
- Status: Offline
Le tutoriel est en cours d'utilisation Création . Check it out pour découvrir la convivialité de la classe. J'espère qu'il était descriptif en...si je ne l'est pas, n'hésitez pas à répondre à l'utilisation tutorial avec toutes les questions que vous avez mai.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bogey
- Bogey


- Inscription: Juil 14, 2005
- Messages: 8211
- Loc: USA
- Status: Offline
Une encore plus simple modèle de la classe a été créé par le chien enragé avec un peu moins de moyens, et même fonctionnalité est arrivé à une manière différente.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 3 messages
- Modérateur: Tutorial Writers
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 4 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
