kros / plates-sections-insertion

板块扩展。将内容插入到区块中。

v1.1 2021-05-19 12:39 UTC

This package is not auto-updated.

Last update: 2024-09-20 03:15:33 UTC


README

本包是模板引擎 Plates 的一个扩展。

在不同的模板中定义区块内容,只需将其插入到任何模板中。现在从控制器中轻松实现。

使用

在模板中定义不同的区块

模板文件夹中的 template.php 文件

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

与往常一样,您还可以定义另一个模板,将第一个模板用作布局,因此它将被插入到 content 区块中

模板文件夹中的 othertemplate.php 文件

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

在不同的模板文件中定义区块内容,以便以后在任意模板中使用

模板文件夹中的 welcome.php 文件

Hello <?= $name ?>

模板文件夹中的 bybye.php 文件

Goodbye

现在我们将其组合起来。在控制器内部(在这种情况下为 index.php 文件)我们可以设置区块的内容。 注意 在设置区块内容之前不能渲染,所以做法是

  • make 模板
  • 设置所有希望设置的区块内容的 setSectionContent
  • 最后 render 渲染模板

index.php 文件

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

您甚至可以使用 pushSectionContentunshiftSectionContent 方法在控制器内部推送或移除区块的内容

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

安装

composer require kros/plates-sections-insertion

要求

独立扩展。

请参阅 composer.json 文件。

许可证

GNU 通用公共许可证 v3.0(有关详细信息,请参阅 LICENSE 文件)。