elmattprofe / kata
Kata 是一个用于加载扩展名为 *.tpl.php 的模板的简单引擎
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2024-09-22 08:22:30 UTC
README
Kata 是一个用于加载扩展名为 "tpl.php" 的模板的简单引擎。
入门指南
要安装 Kata,只需使用 Composer 引入它
$ composer require elmattprofe/kata
加载模板的 PHP 代码
index.php
require('../vendor/autoload.php'); use ElMattProfe\Component\Kata\Kata; // If use a Dotenv add vars in .env putenv('APP_VERSION=1.0'); putenv('APP_URL_BASE=localhost/kataweb/'); putenv('APP_NAME=KataWeb'); // Vars to display in the template $vars = array( "SECTION" => "Welcome", "WEB_NAME" => getenv('APP_NAME') ); // Template is in "resources/views/<section>/<method>", loadView ever at the end code or block code // $vars contains an array with variable values displaying in the template, 'dev' or 'prod' is state of code, if no define defualt is 'dev' Kata::loadView('landing/index', $vars, 'dev');
示例模板
将模板放入 "resources\views\landing" 目录,命名为 "index.tpl.php"
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{APP_NAME}}-{{SECTION}}</title> <link rel="stylesheet" type="text/css" href="{{APP_URL_BASE}}/resources/css/estilos.css?v={{APP_VERSION}}"> </head> <body> {{WEB_NAME}}<br> <a href="{{APP_URL_BASE}}/login">Iniciar Sesión</a> </body> </html>
注意 APP_NAME、APP_URL_BASE 和 APP_VERSION 在代码中默认定义,它们的值从环境变量中获取,因此不需要在 loadView 中通过参数传递它们的值。
包含在 Kata 中的其他函数
扩展
Kata 可以在模板中包含另一个模板,这非常实用,它有助于通过不需要重复常见部分(如页脚、页眉和导航)来简化模板中的代码。
将以下代码放入包含另一个模板的模板中
resources/views/landing/index.tpl.php
@extends('header') <content> <h1>{{SECTION}}</h1> </content> @extends('footer')
resources/views/header.tpl.php
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{APP_NAME}}-{{SECTION}}</title> <link rel="stylesheet" type="text/css" href="{{APP_URL_BASE}}/resources/css/estilos.css?v={{APP_VERSION}}"> </head> <body>
resources/views/footer.tpl.php
<footer> The web: {{APP_NAME}} </footer> </body> </html>
输出
输出是创建模板中变量代码块的一种方式,这些块将使用 extends 调用,将被替换的内容将包含在 @section('name-yield') 和 @endsection('name-yield') 指令中。
将以下代码放入包含另一个模板的模板中
resources/views/landing/index.tpl.php
@extends('web') @section('content') <h1>{{SECTION}}</h1> @endsection('content') @section('footer') @extends('footer') @endsection('footer')
resources/views/web.tpl.php
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{APP_NAME}}-{{SECTION}}</title> <link rel="stylesheet" type="text/css" href="{{APP_URL_BASE}}/resources/css/estilos.css?v={{APP_VERSION}}"> </head> <body> <content> @yield('content') </content> <footer> @yield('footer') </footer> </body> </html>
resources/views/footer.tpl.php
The web: {{APP_NAME}}
条件块
它取决于一个变量,如果存在,则该块的内容将被渲染并显示在屏幕上。
resources/views/landing/index.tpl.php
@extends('web') @section('content') @if('SECTION') <h1>{{SECTION}}</h1> @endif @if('DESCRIPTION') <p>This website is a demonstration of the power of Kata.</p> @endif @endsection('content') @section('footer') @extends('footer') @endsection('footer')