Templates

MaplePHP comes with some working examples out of the box, which I will recreate in the guide below, and you can follow along if you want. I will start by showing the three different template types used by MaplePHP and how they can be utilized.

The index template file

The index view is the main index file, and it can contain one view and multiple partials. It also has access to the "Service Provider." MaplePHP comes with a default index file that you can edit according to your needs. The following is the default index file and will be loaded if you do not manually change it, which is possible in your controllers and middlewares.

Let´s take a look at the resources/index.php file.

<!DOCTYPE html>
<html lang="<?php echo $this->provider()->lang()->prefix(); ?>">
<head>
    <?php echo $this->partial("head")->get(); ?>
</head>
<body>
    <?php echo $this->partial("navigation")->get(); ?>
    <main>
        <?php echo $this->view()->get($args); ?>
    </main>
    <?php echo $this->partial("footer")->get(); ?>
</body>
</html>

The view file

You can only have one view template file at once. It's used for your main content and makes it possible for you to easily change the layout depending on the subject, for example, a blog with a sidebar or regular pages with sections. It does not require much code, and you could add partials directly inside it without any HTML code. However, it is highly recommended that you use it because there will come a time when you will need it. The following is the default view file and will be loaded if you do not manually change it, which is possible in your controllers and middleware.

Let´s take a look at the resources/views/main.php file.

<?php echo $this->partial("main")->get(); ?>
<?php echo $this->partial("form")->get(); ?>

The Partial files

Partials are used just like the name indicates—it is partial content. This could be navigation, sidebar, header, footer, page content, blog post, and so on. You can add as many partials as you need. Below, I will show you one of the pre-installed examples.

Let´s take a look at the resources/partials/ingress.php file.

<div class="article bg-secondary border-bottom">
    <header class="wrapper max-w-screen-md align-center text-lg">
        <?php echo $obj->tagline("Dom")->create("h6")->attr("class", "title"); ?>
        <h1 class="title"><?php echo $obj->name; ?></h1>
        <p><?php echo $obj->content; ?></p>
    </header>
</div>

Last updated