ove*_*dow 8 php static symfony twig
如何在不通过控制器的情况下在树枝模板中调用我的静态函数?
例如:
...
{{ MyStaticClass::getData() }}
...
Run Code Online (Sandbox Code Playgroud)
我的静态课程:
class MyStaticClass {
const v1 = 'Value1';
const v2 = 'Value2';
...
public static function getData() {
...
return $data;
}
}
Run Code Online (Sandbox Code Playgroud)
Sea*_*ean 11
您无法在twig模板中直接调用PHP.您需要创建一个过滤器或函数来执行您正在查找的内容.
$twig = new Twig_Environment($loader, $params);
$twigFunction = new Twig_SimpleFunction('MyStaticClass', function($method) {
MyStaticClass::$method
});
$twig->addFunction($twigFunction);
Run Code Online (Sandbox Code Playgroud)
然后在你的树枝模板中做:
{{ MyStaticClass('getData') }}
Run Code Online (Sandbox Code Playgroud)
当然,上面的例子假设MyStaticClass
在你的任何地方的范围内.
Symfony示例
您必须创建一个树枝扩展.示例如下:
namespace PurpleNeve\Web\PNWebBundle\Extensions;
use PurpleNeve\Web\PNWebBundle\DependencyInjection\CurrencyConverter;
class TwigCurrency extends \Twig_Extension
{
private $converter;
public function __construct(CurrencyConverter $converter)
{
$this->converter = $converter;
}
public function getName()
{
return 'currency';
}
public function getFilters()
{
return array(
'convertCurrency' => new \Twig_Filter_Method($this, 'getConversionBetween')
);
}
public function getConversionBetween($amount, $isoFrom, $isoTo="USD")
{
try {
$value = $this->converter->convertAmount($amount, $isoFrom, $isoTo);
return round($value,2);
} catch(\Exception $e) {
return "?";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我创建的一个扩展示例,用于将货币从一种货币转换为另一种货币.
要实现它,您需要在services.yml中为它创建一个服务对象
parameters:
currency_converter.class: PurpleNeve\Web\PNWebBundle\DependencyInjection\CurrencyConverter
services:
currency_converter:
class: "%currency_converter.class%"
arguments : [@doctrine.orm.entity_manager]
twig.extension.currency:
class: PurpleNeve\Web\PNWebBundle\Extensions\TwigCurrency
tags:
- { name: 'twig.extension' }
arguments : [ @currency_converter ]
Run Code Online (Sandbox Code Playgroud)
然后如上所述,在树枝内我可以调用该类和函数 {{ convertCurrency(55505, 'CAD', 'USD) }}
qwe*_*rtz 10
而不是编写一个Twig扩展,一个更容易/更少膨胀的解决方案有时可以简单地使用静态方法将类的新实例传递给twig.
例如
// ...
$viewVars['MyStaticClass'] = new MyStaticClass();
// ...
$html = $twig->render('myTemplate.html.twig', $viewVars);
Run Code Online (Sandbox Code Playgroud)
在树枝上:
{{ MyStaticClass.getData() }}
Run Code Online (Sandbox Code Playgroud)
一个通用的方法是注册一个名为 Twig 的辅助函数callstatic
来进行调用。
$twig->addFunction(new \Twig_SimpleFunction('callstatic', function ($class, $method, ...$args) {
if (!class_exists($class)) {
throw new \Exception("Cannot call static method $method on Class $class: Invalid Class");
}
if (!method_exists($class, $method)) {
throw new \Exception("Cannot call static method $method on Class $class: Invalid method");
}
return forward_static_call_array([$class, $method], $args);
}));
Run Code Online (Sandbox Code Playgroud)
这种方法的主要优点是它适用于任何类和方法组合。
用法:
{# This will call \Mynamespace\Mypackage\Myclass::getStuff(); #}
{% set result = callstatic('\\Mynamespace\\Mypackage\\Myclass', 'getStuff') %}
Run Code Online (Sandbox Code Playgroud)
它还支持参数:
{# This will call \Mynamespace\Mypackage\Myclass::getStuff('arg1', 'arg2'); #}
{% set result = callstatic('\\Mynamespace\\Mypackage\\Myclass', 'getStuff', 'arg1', 'arg2') %}
Run Code Online (Sandbox Code Playgroud)