ahmetbarut/view-render

一个轻量级的简单模板引擎,没有依赖。

dev-master 2021-10-10 00:24 UTC

This package is auto-updated.

Last update: 2024-09-10 06:34:30 UTC


README

PHP 简单视图引擎

一个轻量级的简单模板引擎,没有依赖。

安装和配置

安装时,必须安装 composer。该包还没有稳定的版本。

    composer require ahmetbarut/view-render

在安装包之后,添加以下配置。包需要 cachetemplate 目录,您需要创建它并指定其路径。

    $view = new \ahmetbarut\View\Render([
        "view" => __DIR__ . "/view",
        'cache' => __DIR__ . '/cache'
    ]);

使用

在完成包的配置之后,让我们首先使用模板来使用它。

在创建的 template 目录中创建一个文件。

    // view/home.php
    {{ $helloWorld }}
    // index.php
    require_once  "vendor/autoload.php";

    $view = new \ahmetbarut\View\Render([
        "view" => __DIR__ . "/view",
        'cache' => __DIR__ . '/cache'
    ]);


    $view->load('home', [
        "helloWorld" => "Hello World !"
    ]);

load 方法加载相应的模板。

指令

指令用于此模板。

extends 和 yield

extends 指令可以用来添加布局。想象一下,您有一个页面,并且不断复制这个页面的整个结构,这会让我们以非常繁琐且不必要的方式编写代码。在这种情况下,extends 指令简化了我们的工作。yield 指令允许获取每个页面都会改变的章节。

示例用法

//layouts.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>  

我们包含了 home.php 页面,并包含了相关布局,然后将我们的页面推入布局中。

// home.php
@extends('layouts')

@section('title')
Hello Template Title
@endSection

@section('content')
hello there, this is content
@endSection

section 和 endSection

section 指令开始章节,endSection 指令结束章节并将它推入顺序。