How to create a ViewHelper in Symfony 2. I read whole the documentation but it doesn't describe any term like that. It just has autoloading and service. http://symfony.com/doc/current/cookbook/index.html
问问题
2467 次
1 回答
4
您只需要创建一个实现您的辅助函数并扩展的类
Symfony\Component\Templating\Helper\Helper
像这样:
namespace Acme\Foo\Helper;
use Symfony\Component\Templating\Helper\Helper;
class MyViewHelper extends Helper {
public function helpMe() {
// do something
return $value;
}
/**
* @inheritdoc
*/
public function getName() {
return "anyCanonicalServiceName";
}
}
然后你必须将你的助手推广为一个带有特殊标签的服务,例如
Resources/config/services.yml
services:
your_service_name:
class: Acme\Foo\Helper\MyViewHelper
# the tag alias "myViewHelper" is later used in the view to access your service
tags:
- { name: templating.helper, alias: myViewHelper }
现在您可以在这样的视图中访问帮助程序:
echo $view['myViewHelper']->helpMe();
于 2014-07-09T09:00:17.510 回答