[英]How to Call Static Function In Symfony2 Twig Template
How to call my static function in twig template without passing through controller?如何在不通过控制器的情况下在树枝模板中调用我的静态函数?
For example:例如:
...
{{ MyStaticClass::getData() }}
...
My Static Class:我的静态类:
class MyStaticClass {
const v1 = 'Value1';
const v2 = 'Value2';
...
public static function getData() {
...
return $data;
}
}
Instead of writing a Twig extension an easier / less bloated solution can sometimes be to simply pass a new instance of the class with the static methods to twig.有时不编写 Twig 扩展,一个更简单/不那么臃肿的解决方案可以是简单地将带有静态方法的类的新实例传递给 twig。
eg例如
// ...
$viewVars['MyStaticClass'] = new MyStaticClass();
// ...
$html = $twig->render('myTemplate.html.twig', $viewVars);
and in twig:在树枝上:
{{ MyStaticClass.getData() }}
You cannot directly call PHP in a twig template.您不能在树枝模板中直接调用 PHP。 You'll need to create a filter or function to do what you're looking.
您需要创建一个过滤器或函数来执行您要查找的操作。
$twig = new Twig_Environment($loader, $params);
$twigFunction = new Twig_SimpleFunction('MyStaticClass', function($method) {
MyStaticClass::$method
});
$twig->addFunction($twigFunction);
Then in your twig template just do:然后在您的树枝模板中执行以下操作:
{{ MyStaticClass('getData') }}
Of course the above example assumes MyStaticClass
is within the scope of wherever you're twig.当然,上面的例子假设
MyStaticClass
在你所在的任何地方的范围内。
Symfony Example Symfony 示例
You must create a twig extentions.您必须创建树枝扩展。 Example below:
下面的例子:
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 "?";
}
}
}
This is an example of an extension I created to convert currency from one currency to another in twig.这是我创建的一个扩展示例,用于在 twig 中将货币从一种货币转换为另一种货币。
To implement it, you need to create a service object for it in your services.yml要实现它,您需要在 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 ]
Then as above, within twig I can call that class and function using {{ convertCurrency(55505, 'CAD', 'USD) }}
然后如上所述,在树枝中我可以使用
{{ convertCurrency(55505, 'CAD', 'USD) }}
A generic approach is to register a Twig helper function named callstatic
to make the call.一个通用的方法是注册一个名为
callstatic
的 Twig 辅助函数来进行调用。
$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);
}));
The main advantage of this approach is that it will work with any class and method combination.这种方法的主要优点是它适用于任何类和方法组合。
Usage:用法:
{# This will call \Mynamespace\Mypackage\Myclass::getStuff(); #}
{% set result = callstatic('\\Mynamespace\\Mypackage\\Myclass', 'getStuff') %}
It also supports arguments:它还支持参数:
{# This will call \Mynamespace\Mypackage\Myclass::getStuff('arg1', 'arg2'); #}
{% set result = callstatic('\\Mynamespace\\Mypackage\\Myclass', 'getStuff', 'arg1', 'arg2') %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.