edevelops/native-tpl-heir 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

edevelops/native-tpl-heir

Composer 安装命令:

composer require edevelops/native-tpl-heir

包简介

Tiny plain PHP native template inheritance library

README 文档

README

Tiny plain PHP native template inheritance library

Latest Stable Version Total Downloads Latest Unstable Version License

Inspired by phpti

Features

  • About 35 lines of code at all
  • No needs to know any template language other than PHP
  • The entire API is just 3 global functions!
  • Overridable blocks are not executed!
  • No ob_* buffering used

Usage

Without composer: just copy/include tiny source directrly.

Using composer:

$ composer require edevelops/native-tpl-heir

Then need to touch NativeTplHeir class to trigger auto-loading. For example

new NativeTplHeir();

API

  • slot(string $name, Closure $render = null) - the output point of the block in the template. $name - unique block name. $render - optional callback for rendering.
  • block(string $name, Closure $render) - defining block to output in the slot of parent template. $name - unique block name. $render - callback for rendering.
  • super() - calling the overridden parent's callback for rendering.

Inheritance

Just include parent template at the end of file.

Example

root.php:

?>
<!DOCTYPE html>
<html>
  <head>
    <title><?php slot('title')?></title>
  </head>
  <body>
    <div id="root">
      <?php slot('body', function () { ?>

        <p>'body' :: root.php</p>

      <?php }) ?>
    </div>
  </body>
</html>

two-columns.php:

block('title', function () { ?>
  Title :: two-columns.php
<?php });

block('body', function () { ?>
  <div id="two-columnts">
    <div id="main">
      <?php slot('main', function () { ?>

        <p>'main' :: two-columns.php</p>

      <?php }) ?>
    </div>
    <div id="side">
      <?php slot('side', function () { ?>

        <p>'side' :: two-columns.php</p>

      <?php }) ?>
    </div>
  </div>
  <div id="footer">
    <?php slot('footer', function () { ?>

      <p>'footer' :: two-columns.php</p>

    <?php }) ?>
  </div>

<?php });

include __DIR__.'/root.php';

index.php:

block('title', function () { ?> 'title' :: index.php <?php });

block('side', function () { ?>

  <p>'side' :: index.php</p>

<?php }); 

block('main', function () { ?>

 <div id="main-index-override">
    <?php super() ?>
  </div>

<?php });

block('main', function () { ?>

  <div id="main-index"> 
    <?php super() ?>
  </div>

<?php });

include __DIR__.'/two-columns.php';

Rendered result (formatted for better readability):

<!DOCTYPE html>
<html>
  <head>
    <title> 'title' :: index.php </title>
  </head>
  <body>
    <div id="root">
      <div id="two-columnts">
        <div id="main">

          <div id="main-index-override">

            <div id="main-index"> 

              <p>'main' :: two-columns.php</p>

            </div>

          </div>

        </div>
        <div id="side">

          <p>'side' :: index.php</p>

        </div>
      </div>
      <div id="footer">

        <p>'footer' :: two-columns.php</p>

      </div>

    </div>
  </body>
</html>

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固