arveres/arveres-template

个人模板引擎库

v1.0 2022-12-22 17:57 UTC

This package is auto-updated.

Last update: 2024-09-22 21:54:31 UTC


README

Arveres Template 是一个具有100%原生PHP基本功能的模板引擎。

安装

要安装 Arveres Template,您可以在终端直接使用 composer 命令

$ composer require arveres/arveres-template

或者您可以在您的 composer.json 文件中添加以下行。

{
    "require": {
        "arveres/arveres-template": "^1.0"
    }
}

然后,执行以下命令

$ composer install

使用示例

让我们假设以下目录结构和文件结构

-- path
    -- to
        -- template
            |-- main.php
            |-- home.php

简单渲染

require_once 'vendor/autoload.php';

use ArveresTemplate\Engine;
use ArveresTemplate\Macros;

//Cria a instância e define o diretório das views
$engine = new Engine('/path/to/template');

//Adiciona classe com funções para o templete engine
$engine->dependencies([new Macros()]);

//Renderiza o template
echo $engine->render('home', ['foo' => 'bar']);

扩展模板

home.php

<?php $this->extends('main', ['title' => 'home page']) ?>

<h1>Home page</h1>
<p>Hello world, <?php echo $this->foo ?>.</p>

在模板中加载内容

main.php

<html>
    <head>
       <title><?php echo $this->title ?></title>
    </head>
    <body>
        <?php echo $this->load() ?>
    </body>
</html>

使用模板引擎中的函数

<body>
    <ul>
        <?php
             foreach ($this->users as $user) {
                echo '<li>' . $this->lower($user->name) . '</li>';
             } 
        ?>
    </ul>

</body>

可用函数列表

  • lower - 将字符串转换为小写
  • upper - 将字符串转换为大写
  • uc - 将字符串的第一个字符转换为大写,其余转换为小写

要求

  • PHP 8.0 或更高版本