DEV Community

Ghulam Mujtaba
Ghulam Mujtaba

Posted on

How to separate PHP logic from template?

Today I learned about how to separate PHP logic from template.

Important, must read

  • As I mentioned in my last post, I explained how to add functions and filter them in the code. However, to separate the logic from the template, we need to create two files: index.view.php for the template and index.php for the logic.

Diving in code

In a fresh vs code project (version 1.90 at the time of working), we need two different files to easily learn the working of code.

  • A template file which contains PHP code that contains a condition inside it in the body tag of HTML

  • Second file is to perform PHP logics in it. It contains array of books and functions etc.

On VS Code Side

  • Firstly create a file named index.view.php that have HTML5 basic code.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>You have read in dark mode </h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS on body tag

  • Also in this file the styling of body tag is present and it's code is as follows.
<style>
        body {
            display: grid;
            place-items: center;
            font-family: sans-serif;
            height: 100px; 
            margin: 20px; 
        }
    </style>
Enter fullscreen mode Exit fullscreen mode

This takes the data present in body in the centre of output screen by giving height, margin and font-family.

PHP condition inside this file

This condition is used to show an unordered list of filtered books at output screen.

<!-- Display filtered books -->
    <ul>
        <?php foreach ($filteredBooks as $book) : ?>
            <li><?= $book['name']; ?> - by <?= $book['author'] ?></li>
        <?php endforeach; ?>
    </ul>
Enter fullscreen mode Exit fullscreen mode

Second file as PHP logic

Second file that is used for logic is named as index.php which contains function to filter books by author name and an associate array of books.

<?php

    function filterBooksByAuthor($books, $author) {
        $filteredBooks = array_filter($books, function($book) use ($author) {
            return $book['author'] == $author;
        });
        return $filteredBooks;
    }

    $books = [
        ['name' => 'Web', 'author' => 'Philip K. Dick', 'purchaseUrl' => 'http://example.com'],
        ['name' => 'OOP', 'author' => 'Andy Weir', 'purchaseUrl' => 'http://example.com'],
        ['name' => 'Database', 'author' => 'Jeffery', 'purchaseUrl' => 'http://example.com']
    ];

    $filteredBooks = filterBooksByAuthor($books, 'Andy Weir');
Enter fullscreen mode Exit fullscreen mode

After adding this, when we start to debug and run the code a blank screen is shown. To resolve this issue add template file index.view.phpin this at the end of above code.

require"Index.view.php";
Enter fullscreen mode Exit fullscreen mode

Now the output will show.

I hope that you have clearly understand everything

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay