alghool/twig

twig for Codeigniter 4

dev-master 2023-09-07 04:42 UTC

This package is auto-updated.

Last update: 2024-09-07 06:43:06 UTC


README

Donate

Twig,一个灵活、快速、安全的Codeigniter 4模板语言

Twig 是一个 PHP 模板语言。

Twig 使用与 Django 和 Jinja 模板语言类似的语法,这些语法启发了 Twig 运行时环境。

Build Status Coverage Status Downloads GitHub release (latest by date) GitHub stars GitHub license

通过 composer 安装

使用 composer install 安装此包

> composer require Alghool/twig

手动安装

下载此仓库,然后通过编辑 app/Config/Autoload.php 并将 Alghool\Twig 命名空间添加到 $psr4 数组中来启用它。例如,如果您将其复制到 app/ThirdParty

$psr4 = [
    'Config'      => APPPATH . 'Config',
    APP_NAMESPACE => APPPATH,
    'App'         => APPPATH,
    'Alghool\Twig' => APPPATH .'ThirdParty/twig/src',
];

配置

运行命令

> php spark twig:publish

此命令将配置文件复制到您的应用命名空间。然后您可以调整它以满足您的需求。默认文件将位于 app/Config/Twig.php

使用库加载

$twig = new \Alghool\Twig\Twig();
$twig->display( 'file', [] );

作为服务使用

$twig = \Config\Services::twig();
$twig->display( 'file', [] );

作为助手使用

在您的 BaseController - $helpers 数组中,添加一个包含您的助手文件名的元素。

protected $helpers = [ 'twig_helper' ];

然后您可以使用此助手

$twig = twig_instance();
$twig->display( 'file', [] );

添加全局变量

$twig = new \Alghool\Twig\Twig();

$session = \Config\Services::session();
$session->set( array( 'name' => 'alghool' ) );
$twig->addGlobal( 'session', $session );
$twig->display( 'file.html', [] );

文件示例

<!DOCTYPE html>
<html lang="es">  
    <head>    
        <title>Example</title>    
        <meta charset="UTF-8">
        <meta name="title" content="Example">
        <meta name="description" content="Example">   
    </head>  
    <body>
        <h1>Hi {{ name }}</h1>
        {{ dump( session.get( 'name' ) ) }}
    </body>  
</html>

如何运行测试

cd vendor\alghool\twig\
composer install
vendor\bin\phpunit