saroj-pant/easy-template

EasyTemplate 是一个简单的原生 PHP 模板引擎。

v1.1.1 2017-04-20 10:19 UTC

This package is auto-updated.

Last update: 2024-08-27 07:53:46 UTC


README

EasyTemplate 是一个简单的原生 PHP 模板引擎。

特性

  • 模板继承和部分
  • 从任何地方插入内容到部分
  • Composer 准备就绪并符合 PSR-2 规范

安装

通过 Composer

$ composer require saroj-pant/easy-template

或在你的 composer.json 文件中

{
    "require": {
        "saroj-pant/easy-template": "^1.0"
    }
}

模板继承和部分

模板继承

模板继承允许一个模板从另一个模板扩展。子模板可以使用“部分”向主模板添加更多内容。

  • 可以使用 $this->$layout() 方法从一个模板扩展到另一个模板。
  • 可以实现多级模板嵌套。

部分

部分用于将内容分组。主模板将定义并输出不同的部分(页眉、页脚、侧边栏等),子模板将添加到这些部分。

  • 主模板中使用 $this->section() 方法定义部分。
  • 可以在同一行中定义和输出一个部分 echo $this->section('header')
  • 使用 $this->startSection()$this->stopSection() 方法将内容添加到部分,如下所示
    // Add content to header section
    $this->startSection('header');
    echo '<div class="header">Header content</div>';
    $this->stopSection();
    

// 将内容添加到页脚部分 $this->startSection('footer'); echo '

页脚内容'; $this->stopSection();

## Usage

### Rules/Limitations

* Rule 1: The child templates should always insert content to sections in the root template. Content which are not added to sections will not display.
* Rule 2: Rule 1 can be ignored only if the non-root template is loaded using **$this->insert()** method from the root template.

### Notes

* All the template methods are called using the **$this** pseudo-variable.
* The template file extension does not need to be passed when calling template methods
* '.' can be used as directory separator in template method argument for readability. Example **$this->include('partials.header')** is same as **$this->include('partials/header')**

### Loading templates

To load a template, we just need to initialize the template engine and call the **render()** method.


#### Step 1: Initialize engine constructor
The template engine is initialized by passing the template directory path as the first argument. In addition, you may wish to specify the template file extension as a second argument which is '.php' by default.

$templateDir = 'path/to/template/directory'; $templateEngine = new SarojPant\EasyTemplate\Engine($templateDir);

#### Step 2: Render template
The **render()** method accepts the template path as the first argument. The second and third arguments are optional and are used to pass data(array) and a boolean flag to display/return template content respectively. The data elements are shared as local variables across all templates.

$templateEngine->render('frontend.index', ['title' => 'Home'];


## Template methods

The template methods are available using $this pseudo-variable in template files.

### layout()
The layout() method is used to inherit from another template and expects the template path as an argument.

$this->layout('layout.1column'); // 推荐 $this->layout('layout/1column'); $this->layout('layout/1column.php');


### insert()
The insert() method will insert a template and expects the template path as an argument.

$this->insert('common.sidebar');


### section()
The section() method defines a section and is used in the root template.

$this->section('header'); $this->section('sidebar');


### startSection() and stopSection()
Content is added to a section by wrapping the content between the **startSection()** and **stopSection()** methods. The **startSection()** requires the section name as the only argument and the **stopSection()** does not have any arguments. The section content is automatically appended to existing content set by parent templates.

$this->startSection('header') echo '

这是页眉

'; $this->stopSection();

$this->startSection('sidebar'); $this->insert('partials.sidebar_menu'); $this->stopSection();


### get()
The get method will return the contents of the given variable name. The second and third arguments are optional. The second argument accepts a default value. The third argument accepts a boolean flag to escape the output using **htmlspecialchars()** and is true by default.

// 如果找到 $pageHeading,则输出,如果没有找到,则输出空字符串 echo $this->get('pageHeading');

// 如果 $pageHeading 未找到,则输出 'Home' echo $this->get('pageHeading', 'Home');


## A simple template system example

The example below will load the login template

### Login page template

<?php // 使用两列布局 $this->layout('layout.2columns');

<?php // 将内容添加到 'main_content' 部分 $this->startSection('main_content'); ?>

<?php $this->stopSection();


### Two column layout template