Created
September 12, 2019 12:29
-
-
Save davidchc/2ddb7f6e3d8f74019f8e1c0367491487 to your computer and use it in GitHub Desktop.
Criar uma classe de template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "Template.php"; | |
/* | |
Exemplo teria uma pasta chamadas templates | |
*/ | |
$template = new Template('templates'); | |
//Passando dados | |
$template->set('user', [ 'name' => 'David CHC']); | |
//Define o nome do arquivo, e retorna para uma variavel | |
$render = $template->render('user'); | |
//imprime o conteudo | |
echo $render; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Template | |
{ | |
private $data = []; | |
private $path; | |
public function __construct($path='') | |
{ | |
$this->path = $path; | |
} | |
public function set($key, $value) | |
{ | |
$this->data[$key] = $value; | |
} | |
public function render($file) | |
{ | |
$filename = $this->path.$file.'.php'; | |
if (file_exists($filename)) { | |
ob_start(); | |
extract($this->data); | |
include $filename; | |
return ob_get_clean(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Usuário: <?php echo $user['name'] ?></h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment