templating

This commit is contained in:
Aleksandr Shelkovin 2025-01-02 10:06:19 +03:00
parent 591bb81ab0
commit ef67839659
7 changed files with 200 additions and 67 deletions

7
engine/ext.php Normal file
View File

@ -0,0 +1,7 @@
<?php
$user = [ 'login' => 'admin', 'pass' => 111];
extract($user);
var_dump($login);

24
engine/file1.php Normal file
View File

@ -0,0 +1,24 @@
<?php
// ini_set('error_reporting', 0);
function one()
{
require_once "lib/file2.php";
sendMessage();
}
function two()
{
include_once "lib/file2.php";
sendMessage();
}
function three()
{
include_once "lib/file2.php";
sendMessage();
}
one();
two();
three();

View File

@ -1,75 +1,35 @@
<?php
$arr = [
['id' => 1, 'name' => 'pop'],
['id' => 2, 'name' => 'hip-hop'],
['id' => 3, 'name' => 'rock'],
['id' => 4, 'name' => 'jazz'],
['id' => 5, 'name' => 'classic'],
];
define('FOLDER_TEMPLATE', './template/');
include "model_box.php";
function render($tpl, $data)
{
extract($data);
$music = [
['id' => 1, 'id_styles' => 5, 'href' => 'https://azbyka.ru/audio/audio1/Muzyka-i-pesni/populjarnaja-klassicheskaja-muzyka/65_iogann_sebastjyan_bah_i_sharlj_guno__sotis_ave_mariya_.mp3'],
['id' => 2, 'id_styles' => 5, 'href' => 'https://azbyka.ru/audio/audio1/Muzyka-i-pesni/populjarnaja-klassicheskaja-muzyka/107_zuppe_uvertyura_k_opere_legkaya_kavaleriya.mp3'],
// ['id'=> 3, 'name'=> 'rock'],
// ['id'=> 4, 'name'=> 'jazz'],
// ['id'=> 5, 'name'=> 'classic'],
];
$str = "";
// include "model_box.php";
foreach ($arr as $item) {
$str .= "<li>" . $item['name'] . "</li>";
// Start output buffering
ob_start();
// Include the template file
include FOLDER_TEMPLATE . $tpl;
// Get the contents of the buffer
$content = ob_get_contents();
// End output buffering and erase the buffer's contents
ob_end_clean();
// Perform any necessary processing on the template content
// For example, you can replace placeholders, apply filters, etc.
// $content = str_replace('{{title}}', 'My Website', $content);
// Output the processed content
return $content;
}
echo ('<ul>' . $str . '</ul>');
$select = "<select id='styles'>
<option value='1'> pop </option>
<option value='2'> hip-hop </option>
<option value='3'> rock </option>
<option value='4'> jazz </option>
<option value='5'> classic </option>
</select>";
$options = "";
foreach ($arr as $item) {
$options .= "<option value=" . $item['id'] . ">" . $item['name'] . "</option>";
}
$select = '<select id="styles">' . $options . '</select>';
echo ($select);
$box = [];
if (
$handle =
opendir('C:\\Lerning\\lerning-php\\itmo-php-course\\engine\\upload')
) {
// echo "Directory handle: $handle\n";
// echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
$ext = pathinfo($entry, PATHINFO_EXTENSION);
// echo $ext ."\n";
if ($ext == 'mp3') {
array_push($box, "C:\\Lerning\\lerning-php\\itmo-php-course\\engine\\upload\\$entry");
}
}
closedir($handle);
}
// var_dump($box);
$playlist = "";
foreach ($box as $item) {
$playlist .= '<audio controls="controls">
<source src="' . $item . '" type="audio/mpeg">
</audio>';
};
// $playlist = "<audio controls>" . $soursec . "</audio>";
echo ($playlist);
$value = ['box' => $box, 'title' => "God save the King!", 'body' => "Happy New Year!", 'auth' => true];
$portial = render('tpl_layout.php', $value);
echo $portial;

73
engine/index_music.php Normal file
View File

@ -0,0 +1,73 @@
<?php
$arr = [
['id' => 1, 'name' => 'pop'],
['id' => 2, 'name' => 'hip-hop'],
['id' => 3, 'name' => 'rock'],
['id' => 4, 'name' => 'jazz'],
['id' => 5, 'name' => 'classic'],
];
$music = [
['id' => 1, 'id_styles' => 5, 'href' => 'https://azbyka.ru/audio/audio1/Muzyka-i-pesni/populjarnaja-klassicheskaja-muzyka/65_iogann_sebastjyan_bah_i_sharlj_guno__sotis_ave_mariya_.mp3'],
['id' => 2, 'id_styles' => 5, 'href' => 'https://azbyka.ru/audio/audio1/Muzyka-i-pesni/populjarnaja-klassicheskaja-muzyka/107_zuppe_uvertyura_k_opere_legkaya_kavaleriya.mp3'],
// ['id'=> 3, 'name'=> 'rock'],
// ['id'=> 4, 'name'=> 'jazz'],
// ['id'=> 5, 'name'=> 'classic'],
];
$str = "";
foreach ($arr as $item) {
$str .= "<li>" . $item['name'] . "</li>";
}
echo ('<ul>' . $str . '</ul>');
$select = "<select id='styles'>
<option value='1'> pop </option>
<option value='2'> hip-hop </option>
<option value='3'> rock </option>
<option value='4'> jazz </option>
<option value='5'> classic </option>
</select>";
$options = "";
foreach ($arr as $item) {
$options .= "<option value=" . $item['id'] . ">" . $item['name'] . "</option>";
}
$select = '<select id="styles">' . $options . '</select>';
echo ($select);
$box = [];
if (
$handle =
opendir('./upload')
) {
// echo "Directory handle: $handle\n";
// echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
$ext = pathinfo($entry, PATHINFO_EXTENSION);
// echo $ext ."\n";
if ($ext == 'mp3') {
array_push($box, "./upload/$entry");
}
}
closedir($handle);
}
// var_dump($box);
$playlist = "";
foreach ($box as $item) {
$playlist .= '<audio controls="controls">
<source src="' . $item . '" type="audio/mpeg">
</audio>';
}
;
echo ($playlist);

19
engine/lib/file2.php Normal file
View File

@ -0,0 +1,19 @@
<?php
$box = [2, 3, 5];
// include_once "../itmo-php-course/engine/file1.php";
// function sendMessage()
// {
// echo "Happy new year! God save the King!";
// }
// three();
// *
// * *
// * * *
// * * * *
// * * * * *
// * * * * * *

21
engine/model_box.php Normal file
View File

@ -0,0 +1,21 @@
<?php
$box = [];
if (
$handle =
opendir('./upload')
) {
// echo "Directory handle: $handle\n";
// echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
$ext = pathinfo($entry, PATHINFO_EXTENSION);
// echo $ext ."\n";
if ($ext == 'mp3') {
array_push($box, "./upload/$entry");
}
}
closedir($handle);
}

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $body; ?> </title>
</head>
<body>
<?php echo $title; ?>
<?php if ($auth): ?>
<div class="logout"><a href="logout.php">exit</a></div>
<?php else: ?>
<div class="login"><a href="login.php">login</a></div>
<?php endif; ?>
<?php foreach ($box as $item): ?>
<audio controls="controls">
<source src="<?php echo $item ?>" type="audio/mpeg">
</audio>
<?php endforeach; ?>
</body>
</html>