Separemos la L贸gica de la plantilla
Antes de continuar con el control t茅cnico de este cap铆tulo, dediquemos un poco de tiempo a discutir la organizaci贸n del c贸digo y por qu茅 podr铆amos querer separar nuestra l贸gica PHP de la vista o HTML.
Que aprenderemos
- Code Organization
- Logic Separation
- require and include
Mover c贸digo
Mover c贸digo php a parte superior, como un primer paso podemos mover el c贸digo php y a la parte superior del archivo html as铆:
C贸digo PHP
<?php
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep?',
'author' => 'Philip K. Dick',
'purchaseUrl' => 'http://example1.com',
'publicationDate' => 1968,
],
[
'name' => 'Four Past Midnight',
'author' => 'Stephen King',
'purchaseUrl' => 'http://example2.com',
'publicationDate' => 1990,
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'purchaseUrl' => 'http://example3.com',
'publicationDate' => 2021,
],
[
'name' => 'The Martian',
'author' => 'Andy Weir',
'purchaseUrl' => 'http://example4.com',
'publicationDate' => 2011,
],
];
// Mandar llamar la **built-in** function de PHP mejor!
$filteredBooks = array_filter($books, function ($book){
return $book['author'] === 'Andy Weir';
});
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
display: grid;
place-items: center;
/* height: 100vh; */
margin: 0;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Libros Recomendados</h1>
<ul>
<?php foreach ($filteredBooks as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name'] ?> (<?= $book['publicationDate']?>) - By <?= $book['author'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
En index.view.php
Como un segundo paso podemos separar el c贸digo html en su propio archivo index.view.php y para llamarlo podemos usar required o include, nota como podemos eliminar ?> del archivo que contiene solo c贸digo php.
C贸digo PHP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
display: grid;
place-items: center;
/* height: 100vh; */
margin: 0;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Libros Recomendados</h1>
<ul>
<?php foreach ($filteredBooks as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name'] ?> (<?= $book['publicationDate']?>) - By <?= $book['author'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
En index.php
C贸digo PHP
<?php
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep?',
'author' => 'Philip K. Dick',
'purchaseUrl' => 'http://example1.com',
'publicationDate' => 1968,
],
[
'name' => 'Four Past Midnight',
'author' => 'Stephen King',
'purchaseUrl' => 'http://example2.com',
'publicationDate' => 1990,
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'purchaseUrl' => 'http://example3.com',
'publicationDate' => 2021,
],
[
'name' => 'The Martian',
'author' => 'Andy Weir',
'purchaseUrl' => 'http://example4.com',
'publicationDate' => 2011,
],
];
// Mandar llamar la **built-in** function de PHP mejor!
$filteredBooks = array_filter($books, function ($book){
return $book['author'] === 'Andy Weir';
});
require "index.view.php";
Listo!
Con esto efectivamente separamos la l贸gica de php con la vista que solo se dedica al render de la pagina que queremos que el usuario pueda ver!
Nota: Como las variables como $filteredBooks pueden ser vista por el c贸digo en la vista!, esto es gracias al la instrucci贸n de php require "index.view.php"; que usamos para mandar llamar nuestra vista.