Partials

Partials are the most commonly used template file type. If you want the same partial with identical content to appear on multiple pages, like a navigation or footer, you could create a middleware and add a partial pointer in it. For more dynamic content that is unique, such as page content or a blog post, you should add it in a controller.

To show a partial you will need to first create a partial template file in the directory resources/partials/ then you set/create a Pointer partial and a Carrot partial. The Carrot partial is only used to set a marker for where you can place a partial while the Pointer partial is used to point a partial file with content to a Carrot partial that exist somewhere in your index or view file.

Set partial

First start with the setPartial method to set a partial. The setPartial method comes with 2 arguments.

$this->provider->view()->setPartial("main.ingress.welcome", [
    "tagline" => "Ingress view partial",
    "name" => "Welcome to MaplePHP",
    "content" => "Get ready to build you first application."
]);

The first argument is a "key string" that is used to point to both file and carrot while the second is used to pass on content to the partial file.

The as I said the first argument is used to make a connection to both file and location to load the file it does come with some minor routing options to.

The simplest way

The "key string" in bellow example will act as a both pointer name and file meaning it expects a template file resources/partials/footer.php to be be loaded at carrot named footer, take a look at the pointer and carrot example:

Pointer - Set partial

$this->provider->view()->setPartial("footer" [...]);

If you set the above partial multiple times it will overwrite the content

Carrot - Read partial

To show the partial above you just add the carrot somewhere in your index or view file.

<footer id="footer">
    <?php echo $this->partial("footer")->get(); ?>
<footer>

Bind and replace content at specific carrot

You can also set a partial and bind it to a specific carrot. The "main" key string part in the bellow example will be the expected carrot name while ingress will be the expected file name resources/partials/ingress.php and "main.ingress" will now act pointer to the partial.

$this->provider->view()->setPartial("main.ingress" [...]);

If you set the above partial multiple times it will overwrite the content.

Carrot - Read partial

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

The ingress will be placed in main. If there already is setted content in main then that content will be replaced with the ingress.

Bind and append content at specific carrot

Works just like above with the exception that if their already is content then the partial will append after that content. The "main" key string part in the bellow example will be the expected carrot name while "ingress" key string name will be the expected file name resources/partials/ingress.php and "welcome" will be subject and all in all "main" will act as the pointer to the partial and "welcome" as a unique identifier.

$this->provider->view()->setPartial("main.ingress.welcome" [...]);

If you set the above partial multiple times it will overwrite the content but if you change the subject then the ingress will be appended under the already added ingress!

Carrot - Read partial

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

Partial files content

The setPartial main.ingress.* will expect the file resources/partials/ingress.php. It could look like bellow.

<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>

Next step we will take a deeper look a the partial content.

Last updated