Hello à tous, IntyView est une class Php5 permettant d'assigner des variables Php à une vue (un template).
Utile lorsque vous concevez votre propre framework MVC.
Je suis en train de pousser cet objet pour en faire un moteur de templates à part entière.
PS: Pour tester convenablement, téléchargez la source, j'espère l'avoir détaillée au mieux :).
Source / Exemple :
<?php
########################################################################################
########################################################################################
###
### IntyView class, permits to assign variables to a view (template) on MVC models.
###
### @name intyView
### @author Edoaurd Kombo <edouard.kombo@live.fr>
###
########################################################################################
########################################################################################
class intyView {
protected $_template;
protected $_delimiter = '#';
function __construct(){}
///////////////////////////////////////////
///////////////////////////////////////////
// PRIVATE FUNCTIONS
///////////////////////////////////////////
///////////////////////////////////////////
private function get($file){
$this->_template = file_get_contents($file); //We open the file
}
private function assign($var , $content){ //We assign variables
return $this->_template = str_replace($this->_delimiter.$var.$this->_delimiter , $content , $this->_template);
}
///////////////////////////////////////////
///////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////
///////////////////////////////////////////
function setDelimiter($delimiter){ //Symbol betwen html variables will be included in
return $this->_delimiter = $delimiter;
}
function export($file , $args = null){ //Get the view for any import (by example mail)
$this->get($file);
if(is_array($args)){
$new_arr = array_keys($args);
foreach($new_arr as $key => $val){
$this->assign($val , $args[$val]);
}
}
return $this->_template;
}
function render($file , $args = null){ //Show the view when this method is called
return eval('?>'.$this->export($file , $args).'<?');
}
}
Conclusion :
//APPLICATION DE L'OBJET
$view = new intyView();
$view->render('test.php' , array('name' => 'Edouard Kombo')); //We show the view directly when the method is called
//We can send this view for example in email
$mail = $view->export('test2.php' , array(
'civility' => 'Monsieur',
'name' => 'Kombo',
'firstname' => 'Edouard'
));
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.