View, Template ..
Bem, estou dando continuidade ao meu mini-fw, e na parte da View, eu fiz o seguinte esquema:
<?php
namespace MVC\Views;
class View {
private $vars = array ( ) ;
public function assign( $var , $value ) {
$this->__set( $var , $value );
}
public function __set ( $var , $value ) {
$this->vars [ $var ] = $value ;
}
public function __get ( $var ) {
if ( isset ( $this->vars [ $var ] ) ) {
return $this->vars [ $var ] ;
} else return null ;
}
public function render ( $template , $path = null ) {
ob_start ( ) ;
$dir = ( ! is_null ( $path ) ? $path : sprintf ( '%s%sTemplates' , __DIR__ , DIRECTORY_SEPARATOR ) ) ;
$filename = sprintf ( '%s%s%s' , $dir , DIRECTORY_SEPARATOR , $template ) ;
if ( file_exists ( $filename ) ) require_once $filename ;
return ob_get_flush();
}
}
Beleza, funciona normalmente, consigo renderizar os arquivos phtml conforme o planejado ..
<?php
$view = new View ( ) ;
$view->title = 'teste';
$view->encoding = 'iso-8859-1';
$view->render( 'index.phtml' ) ;
Input/Output
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=<?php echo $this->encoding ?>" />
<title><?php echo $this->title ?></title>
</head>
<body>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<title>teste</title>
</head>
<body>
</body>
</html>
Só que, quero adicionar um recurso de Helpers, que seriam objetos / functions já escritas para serem usadas na view .. qual seria a melhor maneira de fazer isso ?
Discussão (10)
Carregando comentários...