Luminova Framework

PHP Layout Inheritance

Last updated: 2024-05-22 23:16:22

Luminova framework enables developers to build applications using template engines such as TWIG or SMARTY, or default PHP. However, PHP itself is already a template engine, to enhance its capabilities, we introduce Template Layout in Luminova.

Template Layout offers a straightforward and flexible methods for manipulating templates in the default PHP templating. We prioritize simplicity and cleanliness to ensure optimal performance. Our focus is on addressing the inheritance problem in PHP templating without adding unnecessary complexity since the framework already provides everything you may need to build your application. With Template Layout, you can extend layout sections in your templates easily without sacrificing performance.

  • Class namespace: \Luminova\Template\Layout


In this example we are going to create two files scaffolding.php and card.php in /resources/views/layouts/ directory.The scaffolding will serve as our main layout while the card will serve as a sub layout which our scaffolding will extend it contents.


The file /resources/views/layouts/scaffolding.php demonstrates how to divide your layout into sections, allowing the framework to extend whichever section you want.

<!DOCTYPE html>
<?php $this->begin('head'); ?>
    <link rel="stylesheet" href="<?= asset('css/style.css');?>" />
    <script type="text/javascript" src="<?= asset('js/script.js');?>"></script>
    <title>Website Title</title>
      <?php $this->begin('meta'); ?>
        <meta charset="utf-8">
      <?php $this->end(); ?>
<?php $this->end(); ?>
      <?php $this->begin('section'); ?>
            <p>Website Page Section</p>
            <?= $this->import('card')->extend();?>
      <?php $this->end(); ?>
      <?php $this->begin('footer'); ?>
            <p>&copy; Copyright 2023-2024 <a href="">Luminova</a></p>
            <?php $this->begin('social'); ?>
                <li><a href="#">FB</a></li>
                <li><a href="#">IG</a></li>
            <?php $this->end(); ?>
      <?php $this->end(); ?>


The file /resources/views/layouts/card.php contains another layout which our scaffolding will extend its contents.

        <?php for($i = 0; $i < 5; $i++):?>
                <td>#<?= $i;?></td>
                <td>This is card number <?= $i;?></td>
        <?php endfor; ?>

Extending Samples

In other to extend a layout or a section of layout file, you need to utilize the global helper function layout() to do that, see below examples.

To extend the whole contents of the our scaffolding layout:

<?= layout('scaffolding')->extend();

To extend a specific layout section from scaffolding, you will need to pass the section name in extend method parameter.

<?= layout('scaffolding')->extend('section');

Using nested extending, you must specify the nexting using dot annotation.Example: here we extend social, the child section footer which is the parent section.

<?= layout('scaffolding')->extend('');


To define a layout sections, you must use $this keyword within the layout files whether you enabled view isolation or not.See the Samples reference above.


Get the singleton instance of Layout.

public static getInstance(): self

Return Value:

self - Return the instance of Layout class.


Import a layout file file or section into another layout file.

protected static import(string $layout): static


$layoutstringFile name without extension (.php)

Return Value:

static - Return the instance of Layout class.


Set the layout file name to extend.

public layout(string $layout): self


$layoutstringFile name without extension (.php).

Return Value:

self - Return the instance of Layout class.



The begin method, indicate where a new layout section begins till end method is called.

protected begin(string $name): void


$namestringSection name to begin.


The end method, indicate that the current layout section has ended.

protected end(string|null $name = null): void


$namestring|nullOptional section name to end.



This method allows you to inherit or extend a layout section into another view file.When you pass NULL, you will inherit all the contents of the layout file passed in layout method, (e.g: layout('scaffolding')->extend(null);)

public extend(string|null $section = null): string


$sectionstring|nullSection name to extend or pass null to load all sections.

Return Value:

string - Return the extended or inherited layout contents.


The get method is equivalent with passing NULL to the extend method.This will inherit all the contents of the layout file ignoring the defined start and end sections..

public get(): string

Return Value:

string - Return the inherited layout contents.