Skip to content

Plates PHP Extension to easily insert sections into a template.

License

Notifications You must be signed in to change notification settings

kros/PlatesSectionsInsertion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Plates Extension: Easily insert sections into Plates templates.

This package is an extension for the template engine Plates.

Define sections content in diferent templates and just insert them into any template. Now it's easy to do it from the controller.

Usage

Define different sections in your template:

template.php file in templates folder

<html>
<body>
<div id='div_header'><?= $this->section('up_section') ?></div><hr />
<?= $this->section('content') ?>
<hr /><div id='div_footer'><?= $this->section('bottom_section') ?></div>
</body>
</html>

As allways, you can define another template and use the first one as a layout, so it will be inserted in the content section:

othertemplate.php file in templates folder

<?php $this->layout('template.php'); ?>
This is the main body of other template

Define sections contents in different template files so you can use them later in any template:

welcome.php file in templates folder

Hello <?= $name ?>

bybye.php file in templates folder

Goodbye

Now we put it together. From inside the controller (index.php file in this case) we can set the content of a section. Remember you can not render before having set the sections content, so the way to do it is:

  • make the template
  • setSectionContent of all the sections you want
  • render finally render the template

index.php file

<?php
include "vendor/autoload.php";
use League\Plates\Engine;
use Kros\PlatesSectionsInsertion\SectionsInsertion;
  
$engine = new Engine('route/to/templates');
$engine->loadExtension(new SectionsInsertion());

$t = $engine->make('othertemplate'); /* make the template
$t->setSectionContent('up_section', 'welcome', ['name'=>'John Doe']); // set content for 'header_section' section (with params)
$t->setSectionContent('bottom_section', 'bybye'); // set content for 'bottom_section' (without params)
echo $t->render(); // finally render the template
  

You can even push or unshift the content of a section from inside the controller using the pushSectionContent and unshiftSectionContent methods:

...
$t->pushSectionContent('up_section', 'welcome', ['name'=>'Mary May']); // push the new content for 'up_section' section behind the actual content.
$t->unshiftSectionContent('bottom_section', 'bybye'); // unshift the new content for 'bottom_section' section before the actual content.

Installation

composer require kros/plates-sections-insertion

Requirements

Stand alone extension.

See composer.json file.

License

GNU General Public License v3.0 (see the LICENSE file for details).

About

Plates PHP Extension to easily insert sections into a template.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages