-
Notifications
You must be signed in to change notification settings - Fork 0
06_Views
Hossein Pira edited this page Sep 8, 2024
·
1 revision
In controllers or functions, you can easily call your theme and display the site template to the user.
For this, we use the view function that was added in version 1.0.0. This function can also send data.
<?php
namespace SyntoraPHP\App\Controllers;
class HomeController
{
public function index()
{
view("index");
}
}
When you write such a code, the index.php file will be displayed in the views folder. From . Use to refer to folders.
<?php
namespace SyntoraPHP\App\Controllers;
class BlogController
{
public function post()
{
view("blog.post");
}
}
Here the post.php file from the views/blog folder is displayed. Use an array in the view function to send data to the view.
<?php
namespace SyntoraPHP\App\Controllers;
class BlogController
{
public function post($id)
{
$tags = [
'php',
'web',
'back-end'
];
view("blog.post", [
'id' => $id,
'title' => 'what is php?',
'tags' => $tags
]);
}
}
To display them, just write the key name of the array as a variable.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SyntoraPHP - <?= $title ?></title>
</head>
<body>
<h1>Title: <?= $title ?></h1>
<br />
<h2>Post ID: <?= $id ?></h2>
<br />
<h2>Tags:</h2>
<ul>
<?php
foreach ($tags as $tag) {
echo "<li>$tag</li>";
}
?>
</ul>
</body>
</html>
- 1 - Installation
- 2 - Routing
- 3 - HTTP Request
- 4 - CORS
- 5 - Environment Variables
- 6 - Views
- 7 - Database
- 1 - Installation
- 2 - Routing
- 3 - HTTP Request
- 4 - CORS
- 5 - Environment Variables
- 6 - Views
- 7 - Database