1. Go to this page and download the library: Download jonatasrt/template library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jonatasrt / template example snippets
onatasrt\view\Template;
onatasrt\view\Template;
// antes
use raelgc\view\Template;
// depois
use jonatasrt\view\Template;
$produtos = [
["nome" => "Sabão em Pó", "quantidade" => 15],
["nome" => "Escova de Dente", "quantidade" => 53],
["nome" => "Creme Dental", "quantidade" => 37],
];
foreach ($produtos as $p) {
$tpl->NOME = $p["nome"];
$tpl->QUANTIDADE = $p["quantidade"];
$tpl->block("BLOCK_DADOS"); // "imprime" uma linha
}
$tpl->show();
foreach ($produtos as $p) {
$tpl->NOME = $p["nome"];
$tpl->QUANTIDADE = $p["quantidade"];
$tpl->block("BLOCK_DADOS");
}
// BLOCK_PRODUTOS não precisa ser chamado
$tpl->show();
foreach ($produtos as $p) { // se $produtos estiver vazio...
$tpl->NOME = $p["nome"];
$tpl->QUANTIDADE = $p["quantidade"];
$tpl->block("BLOCK_DADOS");
}
$tpl->show(); // ...a mensagem do FINALLY aparece sozinha
$estados = ["RS" => "Rio Grande do Sul", "SC" => "Santa Catarina", "PR" => "Paraná"];
$atual = "SC";
foreach ($estados as $sigla => $nome) {
$tpl->SIGLA = $sigla;
$tpl->NOME = $nome;
if ($sigla == $atual) $tpl->SELECTED = "selected";
else $tpl->clear("SELECTED"); // limpa para a próxima volta
$tpl->block("BLOCK_ESTADO");
}
$tpl = new Template("base.html");
$tpl->addFile("CONTEUDO", "miolo.html");
$tpl->FULANO = "Maria";
$tpl->show();
$conteudo = $tpl->parse(); // retorna a string final
file_put_contents("arquivo.html", $conteudo);
// echo $tpl; também funciona (equivale a $tpl->show())
$tpl->USUARIO = $usuario;
$tpl = new Template("relatorio.csv");
header("Content-Type: text/csv");
$tpl->show();
$tpl = new Template("base.html", true); // modo "accurate" (mais lento, saída exata)
html
<!-- BEGIN BLOCK_PRODUTOS -->
<table>
<!-- BEGIN BLOCK_DADOS -->
<tr><td>{NOME}</td><td>{QUANTIDADE}</td></tr>
<!-- END BLOCK_DADOS -->
</table>
<!-- END BLOCK_PRODUTOS -->
html
<!-- BEGIN BLOCK_DADOS -->
<tr><td>{NOME}</td><td>{QUANTIDADE}</td></tr>
<!-- END BLOCK_DADOS -->
<tr><td colspan="2">Nenhum produto cadastrado.</td></tr>
<!-- FINALLY BLOCK_DADOS -->
html
<select name="estado">
<!-- BEGIN BLOCK_ESTADO -->
<option value="{SIGLA}" {SELECTED}>{NOME}</option>
<!-- END BLOCK_ESTADO -->
</select>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.