resist/twigphpfunctions

允许在模板中使用PHP函数的小型Twig扩展

1.0.0 2021-04-18 11:04 UTC

This package is auto-updated.

Last update: 2024-09-18 19:23:36 UTC


README

在模板中允许使用PHP函数

安装

composer require resist/twigphpfunctions

使用

构造函数参数是一个包含内置PHP函数名称的字符串数组。

new TwigPhpFunctions(['ceil', 'floor']);

在Symfony框架中

config/services.yaml

parameters:
	app.allowed_php_functions_in_templates: ['ceil', 'floor']
	
services:
	resist\Twig\TwigPhpFunctions:
			tags: ['twig.extension']
			arguments:
				$functions: '%app.allowed_php_functions_in_templates%'

无框架

use resist\Twig\TwigPhpFunctions;
use const ALLOWED_PHP_FUNCTION_IN_TEMPLATES;

/** @var \Twig\Environment $twig */
$twig->addExtension(new TwigPhpFunctions(ALLOWED_PHP_FUNCTION_IN_TEMPLATES));