Je viens exposer ma petite classe qui me serre à traiter des fichiers de vue (phtml) dans mon pseudo framework. Histoire de savoir si vous la trouver fonctionnelle ?
class view
{
private $filename ;
private $vars ;
public function __construct( $vars = array() )
{
$this-> vars = $vars ;
}
public function __set( $name, $value )
{
$this-> vars[$name] = $value ;
}
public function __get( $name )
{
$result = null ;
if( array_key_exists( $name, $this-> vars ) )
{
$result = $this-> vars[$name];
}
return $result ;
}
public function __isset( $name )
{
return isset( $this-> vars[$name] );
}
public function __unset( $name )
{
if( array_key_exists( $name, $this-> vars ) )
{
unset( $this-> vars[$name] );
}
}
public function __toString()
{
return $this-> render();
}
public function open( $filename )
{
if( file_exists( $filename ) )
{
$this-> filename = $filename ;
}
else
{
$this-> filename = null ;
trigger_error( "View \"$filename\" not found." );
}
}
public function render()
{
$result = '' ;
if( !is_null( $this-> filename ) )
{
ob_start();
require $this-> filename ;
$result = ob_get_contents();
ob_end_clean();
}
return $result ;
}
}
Vue main :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $this-> title ?></title>
</head>
<body>
<div id="main">
<?php echo $this-> content ?>
</div>
</body>
</html>
vue content :
<div id="main">
<h1><?php echo $this-> message ?></h1>
</div>
Exploitation PHP :
// CONTENU :
$pageContent = new view();
$pageContent-> open( './view/contenu.phtml' );
$pageContent-> message = "Ceci est un message" ;
// MAIN :
$pageMain = new view();
$pageMain-> open( './view/main.phtml' );
$pageMain-> title = "Titre du site"
$pageMain-> content = $pageContent ;
echo $pageMain ;
En espérant aider certain.Ciao