Download the PHP package lorenzosanzari/mozaiku without Composer
On this page you can find all versions of the php package lorenzosanzari/mozaiku. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lorenzosanzari/mozaiku
More information about lorenzosanzari/mozaiku
Files in lorenzosanzari/mozaiku
Package mozaiku
Short Description Plain PHP Template Inheritance Class
License MIT
Informations about the package mozaiku
Mozaiku
Plain PHP Template Inheritance
Inspired by several hierarchical template engines like Twig or Blade, Mozaiku offers to you the possibility of building hierarchical templates using simply plain PHP language. It is a very simple and lightweight class, with very small footprint.
License
Mozaiku is released under the MIT license.
Install via Composer
If you are using Composer, you can run the following command:
OR you can download the zip file directly from Github and extract them to your web directory.
Template inheritance
What is template inheritance? Template inheritance is an elegant way to make and compose reusable HTML layouts in a web application. It is much more powerful than traditional "include" (bad) practice (like include a header and footer file).
With Mozaiku, you can implement template inheritance simply using plain PHP language. There is no need to learn another template language.
Having a main template, you can conceptually define a "child" view that extends the main layout. This means that it will have the same general appearance as the layout, but will redefine some sections with its own content. In other words, in the child view, you only need to redefine the sections you want to change with respect to the theme (section override) and the whole page will inherit all the rest of the content from the "parent" theme.
Sections
In a layout, you can define a new section simply by writing:
The sections can also be nested, to allow a more detailed management of the contents to be overwritten. In the same layout it is not possible to define multiple sections with the same name.
Parent section content
In a section it is possible to insert a new content, but also to recall the content of the same section of the parent layout, through the directive:
Extending a parent layout
Example:
Warning: the $this->extendsView('my_parent_view.php') directive MUST be placed at start of the child page!
Rendering 'page.php' with Mozaiku, you will obtain the following output:
If in your template you simply want to show the contents of a section populated by child views, you can simply write:
Include partial view in your layout
You can include some partial view in a section of your layout (or in any point, if your view is not a child view), simply by writing:
Rendering or Capturing Output
The render() function
allows you both to show the final output, and to capture its contents in a variable (string). If third parameter (e.g $return) is equal true, $output will contain all the output from the processed view.
Content Stacks
Mozaiku allows you to push contents to named stacks which can be rendered at some point in your layout. This is very useful to inject JavaScript or CSS tags or script required by your child views:
Render the stack: In your html layout:
Debug and strict mode
You can use Mozaiku in debug mode to display debugging information related to the status of the internal stacks of section overrides:
The strict mode will give you error messages about the optimal way to write contents within the views:
For any further doubts, see the example files included in the project. ;-)