devknown/twig-import-function
最新稳定版本:1.0.0
Composer 安装命令:
composer require devknown/twig-import-function
包简介
Call (almost) any PHP function from your Twig (v3) templates.
README 文档
README
Call (almost) any PHP function from your Twig (v3) templates.
Requirements
PHP 7.3 and later. Twig 3.x
Composer
You can install the bindings via Composer. Run the following command:
composer require devknown/twig-import-function
To use the bindings, use Composer's autoload:
require_once('vendor/autoload.php');
Manual Installation
If you do not wish to use Composer, you can download and include the ImportFunctionExtension.php file.
require_once('./src/Devknown/Twig/Extension/ImportFunctionExtension.php');
Getting Started
Simple usage looks like:
// 1. Setup twig 3.x $loader = new \Twig\Loader\FilesystemLoader('/path/to/template'); $twig = new \Twig\Environment($loader); // 2. Load extension $extension = new \Devknown\Twig\Extension\ImportFunctionExtension(); // 3. Import a function $extension->import('uniqid'); // -> Or multiple functions function my_custom_function($name) { echo "Your name is: " . $name; } $extension->import(['uniqid', 'file_exists', 'my_custom_function']); // Note: in this example, the 'my_custom_function..' must be accessible globally // 4. Add extension $twig->addExtension($extension); // 5. Render as normal echo $twig->render('home.html', ['the' => 'variables', 'go' => 'here']);
the template file home.html (/path/to/template/home.html) would look like:
<div> Hi, I am unique: <b>{{ uniqid() }}</b>. <br/> {{ my_custom_function('Devknown') }} </div>
统计信息
- 总下载量: 49.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04