timworx/leaf-twig 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

timworx/leaf-twig

最新稳定版本:v1.0.0

Composer 安装命令:

composer require timworx/leaf-twig

包简介

Leaf PHP Framework adaptation of twig/twig package

README 文档

README

Latest Stable Version Twig Version

The standalone version of Symfony's Twig templating engine for use in the Leaf framework.

This package is oriented towards the Leaf/Blade package.

Installation

Install using composer:

composer require timworx/leaf-twig

Usage

Create a Twig instance by passing it the folder(s) where your template files are located, and the Twig options like a cache folder. Render a template by calling the render method. More information about the Twig templating engine can be found on https://twig.symfony.com/.

use Leaf\Twig;

$twig = new Twig(['templates'], ['cache' => 'var/cache']);

You can also initialise it globally and configure the instace later.

$twig = new Twig;

// somewhere, maybe in a different file
$twig->configure(['templates'], ['cache' => 'var/cache']);

// alternative
$twig->config(['templates'], ['cache' => 'var/cache']);
echo $twig->render('index.html.twig', ['name' => 'John Doe']);
exit;

We can have this as our template index.html.twig

<!Doctype html>
<html>
    <head>
        <title>{{ name }}</title>
    </head>
    <body>
        <div class="container">{{ name }}</div>
    </body>
</html>

You can extend Twig using TwigFilters, TwigFunctions and Extensions:

// Function
$twig->addFunction('md5', function ($string) {
    return md5($string);
});

// Filter
$twig->addFilter('md5', function ($string) {
    return md5($string);
});

// Extension
$twig->addExtension(new \App\Extension\MyExtension()); // Your own created extension

Which allows you to use the following in your blade template:

MD5 hashed string with function: {{ md5(name) }}
MD5 hashed string with filter: {{ name|md5 }}

You can also set global variables:

$twig->addGlobal('myGlobal', 'The Value');

If you want to use additional Twig functions, you can access the Twig instance.

$twig->twig(); // The Twig environment instance

For more Twig directives check out the original documentation.

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-21