php 在 twig 中使用自定义函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13277444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a custom function in twig
提问by ed209
In my template I want to output the server timezone.
在我的模板中,我想输出服务器时区。
My template has something like
我的模板有类似的东西
{{ getservertimezone }}
Then in the services.yml config for that bundle I have
然后在该包的 services.yml 配置中我有
my.twig.extension:
class: My\WebsiteBundle\Extensions\Twig\SomeTemplateHelper
tags:
- { name: twig.extension }
And my SomeTemplateHelperlooks like
我的SomeTemplateHelper样子
namespace My\WebsiteBundle\Extensions\Twig;
class SomeTemplateHelper extends \Twig_Extension
{
public function getFilters()
{
return array(
'getservertimezone' => new \Twig_Filter_Method($this, 'getServerTimeZone'),
);
}
public function getServerTimeZone()
{
if (date_default_timezone_get()) {
return date_default_timezone_get();
} else if (ini_get('date.timezone')) {
return ini_get('date.timezone');
} else {
return false;
}
}
public function getName()
{
return 'some_helper';
}
}
But I can't call this method unless it's used like a filter {{ someval | getservertimezone }}, is there a way to just do a straigh {{ getservertimezone() }}call?
但是我不能调用这个方法,除非它像过滤器一样使用{{ someval | getservertimezone }},有没有办法直接{{ getservertimezone() }}调用?
回答by Vadim Ashikhman
Use getFunctions()instead of getFilters()
使用getFunctions()代替getFilters()
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('server_time_zone', array($this, 'getServerTimeZone')),
);
}
Twig filters are used to filter some value.
树枝过滤器用于过滤某些值。
{{ "some value" | filter_name_here }}
Btw, you can define both filters and functions in the same class.
顺便说一句,您可以在同一个类中定义过滤器和函数。
回答by Elnur Abdurrakhimov
Instead of getFilters, override getFunctionsand use Twig_Function_Methodinstead of Twig_Filter_Method.
代替getFilters,覆盖getFunctions并使用Twig_Function_Method代替Twig_Filter_Method。
回答by twigger
In the newer versions of Twig he should be using Twig_SimpleFunction instead of Twig_Function_Method and Twig_SimpleFilter instead of Twig_Filter_Method, since Twig_*_Method are deprecated (I am using Twig v. 1.24.0 along with Symfony 2.8.2)
在较新版本的 Twig 中,他应该使用 Twig_SimpleFunction 而不是 Twig_Function_Method 和 Twig_SimpleFilter 而不是 Twig_Filter_Method,因为 Twig_*_Method 已被弃用(我正在使用 Twig v. 1.24.0 和 Symfony 2.8.2)
回答by chebaby
Symfony ^2.6 - twig ^1.38
Symfony ^2.6 - 树枝 ^1.38
See example below
请参阅下面的示例
AppExtension.php
应用扩展.php
namespace Your/NameSpace;
class AppExtension extends \Twig_Extension {
public function getFilters()
{
return array(
new \Twig_SimpleFilter('cdn_asset_filter', array($this, 'cdn_asset_filter')),
);
}
public function getFunctions()
{
return array(
new \Twig\TwigFunction('cdn_asset_function', array($this, 'cdn_asset_function')),
);
}
public function cdn_asset_filter($path)
{
return "https://cdn.example.com/$path";
}
public function cdn_asset_function($path)
{
return "https://cdn.example.com/$path";
}
}
view.html.twig
视图.html.twig
// Filter
<img src="{{ 'path/to/image.png'|cdn_asset_filter }}">
// result : <img src="https://cdn.example.com/path/to/image.png">
// Function
<img src="{{ cdn_asset_function('path/to/image.png') }}">
// result : <img src="https://cdn.example.com/path/to/image.png">
app/config/services.yml
应用程序/配置/服务.yml
services:
my_global_filters_and_functions:
class: Your/NameSpace/AppExtension
tags:
- { name: twig.extension }
This is how i used custom functions in Twig in one my old projects, i don't know if it's a best practice, but it worked for me.
这就是我在旧项目中使用 Twig 中自定义函数的方式,我不知道这是否是最佳实践,但它对我有用。
Resources : Twig Documentation - Extending Twig
资源:Twig 文档 -扩展 Twig

